tags:

views:

214

answers:

5

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.

Any help appreciated.

Confused!

+1  A: 

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.

lubos hasko
thx lubos ... do you have an example of your second option (clean way)?
A: 

You can access function of Class A from B via interface using interface polymopisian .Hope this help.

class B {

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

}

Interface IA { void DoSomething(); }

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

}

mtt
this code above is OK ... the only thing that will not work is passing (IA a) to the DoSomethingOnA method. Everything else makes sense to me except the passed in Interface.
in this example does classB assembly reference classA assembly? I am after a solution where the assemblies are not referenced but classA and execute method in classB
A: 

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.

Can these both projects reference another (third) proxy project/assembly that will define only interfaces?
lubos hasko
yes of course. as the whole solution grows, there will be more of these cases arising. so yes a dedicated project just for interfaces could be created.
A: 

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 have 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(){// }

}

mtt
how do you call DoSomethingOnB for example ... I see that you have to pass to it the Interface? How is this done?
The caller from other assembly calls that function like thisA myA=new A();B myB=new B();myA.DoSomethingOnB(myB);As you can see ,those classes are absolute decuple to each other and class A can call class B function without any reference to it .
mtt
If the classes do not reference one another I don't understand how you can write "B myB = new B()" in Class A. Without a reference to Class B the above line is invalid.
It doesn't mean B myB=new B() is in class A ,i mean in different assembly that consume those classes where class B function is actually called at this time.Can't instantiate class B on A without its ref but can know its function via interface within class A.Now i am pointing out on that.
mtt
whatever assembly has the code A myA=new A(); B myB=new B(); myA.DoSomethingOnB(myB); appears to need a reference to both projects containing the classes. Sorry but I am confused with this implementation. Can you reply with more detail ... i.e. how it works to print something on the console.