How to create an interface between two projects, no link

How do two classes in separate projects interact with each other?

If ClassA refers to ClassB, I can access the methods of ClassB in ClassA ... How can I use interfaces to access methods of ClassA in ClassB?

Do the classes even need to be linked if I use interfaces?

Can someone please provide me with an example?

Coding in C # 2.0.


I mean classes. If I have two projects that are not referenced, but I would like to access the methods in the class of one from the other ... how can I achieve this using interfaces.

I was told that this is what I should be using, however I am having a hard time figuring out how to implement the interface if no projects are referenced.

+1


source to share


3 answers


I am assuming that you mean assemblies, not classes.

You have two options, you use the System.Reflection namespace (the dirty way) and then you don't even need any interfaces, just call the methods through reflection.



System.Reflection.Assembly.LoadFile("MyProject.dll").GetType("MyProject.TestClass").GetMethod("TestMethod").Invoke();

      

Or in a clean way, create AssemblyZ with only interfaces and let the classes in both assemblies (A and B) implement those interfaces.

+2


source


You can access a function of class A from B through an interface using interface polymorphism.



class B
{

   Public sub DoSomethingOnA(IA a )
   {
        a.DoSomething();
   }

}

Interface IA
{
    void DoSomething();
}

Class A : IA
{
    void DoSomething()
   {
       //
   }

}

      

0


source


  • In the link assembly, just define the interfaces:

    Public Interface IA { void DoSomething(); }
    
    Public Interface IB { void DoSomething(); }
    
          

  • Reference An assembly reference from another assembly that has class A:

    class A : LinkAssembly.IA {
    
    Public sub DoSomethingOnB(IB b ) { b.DoSomething(); }
    
    Public sub DoSomething(){// }
    
    }
    
          

  • Same for Class B as A:

    class B : LinkAssembly.IB {
    
    Public sub DoSomethingOnA(IA a ) { a.DoSomething(); }
    
    Public sub DoSomething(){// }
    
    }
    
          

0


source







All Articles