jasper
jasper

Reputation:

How do you create an Interface between two projects, not referenced

How can two classes in separate projects communicate between one another?

If ClassA references ClassB I can access methods of ClassB in ClassA... How can I make use of Interfaces to access methods of ClassA in ClassB?

Indeed do the classes even need to be linked if I make use of Interfaces?

Can someone please provide me with an example?

Coding in C# 2.0.


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

It has been alluded to me that this is what I should use however I am having a hard time understanding how to implement an interface if the projects are not referenced.

Upvotes: 1

Views: 3466

Answers (3)

mtt
mtt

Reputation: 113

  1. In Link Assembly, just define interfaces:

    Public Interface IA { void DoSomething(); }
    
    Public Interface IB { void DoSomething(); }
    
  2. Reference Link Assembly from another assembly that has Class A:

    class A : LinkAssembly.IA {
    
    Public sub DoSomethingOnB(IB b ) { b.DoSomething(); }
    
    Public sub DoSomething(){// }
    
    }
    
  3. Same for Class B like A:

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

Upvotes: 0

mtt
mtt

Reputation: 113

You can access function of Class A from B via interface using interface polymorphism.

class B
{

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

}

Interface IA
{
    void DoSomething();
}

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

}

Upvotes: 0

lubos hasko
lubos hasko

Reputation: 25052

I assume you mean assemblies, not classes.

You have two options, either you use System.Reflection namespace (dirty way) and then you don't even need to have any interfaces, just invoke methods via reflection.

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

Or clean way, create AssemblyZ just with interfaces and let classes in both assemblies (A & B) to implement these interfaces.

Upvotes: 2

Related Questions