开发者

Call methods from Managed C++ dll object

I load my dll in C# with

Assembly assembly = Assembly.LoadFrom(dllPath); // late binding
Type classType = assembly.GetType("Namespace.Classname"); 
object readerInterface = Activator.CreateInstance(classType);

but how can I access to m开发者_运维技巧y methods/members in readerInterface without

type.InvokeMember("Methodname", BindingFlags.InvokeMethod |             
    BindingFlags.Instance | BindingFlags.Public, null, readerInterface, null);

--> in form of readerInterface.write(); ???

Thank you very much!

greets leon22


assuming that you can't just reference the assembly in your project C#... have the C++/CLI object implement an interface and cast it to that interface, then just use it as normal.

1) declare your interface in C# using whatever methods are appropriate

public interface IFoo
{
    SomeMethod()
}

2) Implement the interface on your C++/CLI object

3) cast the object that you created through reflection to that interface

object readerInterface = Activator.CreateInstance(classType);
IFoo myFoo = readerInterfces as IFoo;


In c# 3 you must use reflection or have the object implement a known interface.

In C# 4 you can use dynamic instead. (will still use reflection but with nicer syntax)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜