Jump to content
  • Advertisement
Sign in to follow this  

[.net] 2-way communication between assemblies -- is it possible?

This topic is 4298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have assembly A and assembly B. A is written in C++/CLI and B in C#. I wish A to be able to call functions and classes in B, and to pass a class to a function/method in B, and let the function/method in B access it's methods.. Another scenario: I have an assembly called RenderLib. It has an interface (IRenderer), which two classes in other assemblies should implent (DXRendererm & GLRenderer). I want the users of RenderLib to call a function in it, returning an IRenderer. Is this possible? All help appreciated.. :)

Share this post


Link to post
Share on other sites
Advertisement
Yeah...


// In c#
public interface IRenderer
{
void SomeMethod(SomeManagedType argument);
}

//----------------------------------------------------

// In c++
#include "Ogre.h" //or whatever

using namespace MyInterfaceNamespace;

public ref class DXRenderLib : public IRenderer
{
public:

void SomeMethod(SomeManagedType^ argument)
{
Console::WriteLine("I got " + argument->ToString());

// Do unmanaged stuff if you want
Ogre::Light *light = mSceneMgr->createLight(whatever);
light->setDiffuse(whatever);

//etc...
return;
}
};









What is it that's giving you problems?

EDIT:

You can also load the assembly and instantiate a type by name...

Assembly asm = Assembly.LoadFrom("MyAssembly.dll");
IRenderer renderer = asm.CreateInstance("MyRendererNamespace.GLRenderer");

SomeManagedType arg = new SomeManagedType();
renderer.SomeMethod(arg);


Share this post


Link to post
Share on other sites
My problem is.. Who should reference who? If RenderLib has the interface, then DxRenderer should reference it, right? This means I can't reference DxRenderer from RenderLib, which means I can't call the functions in it.. Or am I just completely wrong here?

I'll try with dynamic loading though..

Share this post


Link to post
Share on other sites
What's the point in implementing an interface if you are going to hard code references to it?

You should provide implementations that can be dynamically loaded.

Share this post


Link to post
Share on other sites
Quote:
Original post by ernow
You could always move the shared interfaces (and classes) to a third assembly...

Cheers


This is exactly what should be done.
In cases of 2-way communication that isn't simply return values one way, interfaces should almost always be used. Otherwise you can easily run into versioning conflicts and the like

Share this post


Link to post
Share on other sites
Use 3 assembly´s

The Base Assambly
- The startup and pluginloader
- The interface declaration

2 Plugins
- reference the base assembly
- All comunication beween the assamblys via the interfaces from the base assambly.

all comunication between the assamblys must by done via the interfaces of the base assambly.

You can also use reflaction but its slow and less elegant.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!