简体   繁体   English

如何在C#中通过反射实现接口

[英]how to implement interface through reflection in c#

My Code is like 我的代码就像
Assembly assemblyX = Assembly.Load("xyz.abc.DataObjects"); Type t = assemblyX.GetType("xyz.abc.DataObjects." + ValueStr); Everything is good till this point. 到目前为止,一切都很好。 Now i want to implement interface on 't' which is 'xDataObject' and require a generic ie "CurrentDataObject" in < > operators like xDataObject<CurrentDataObject> . 现在,我想在't'上实现接口,该接口为'xDataObject',并且在xDataObject<CurrentDataObject>这样的<>运算符中需要通用的即“ CurrentDataObject”。 This interface consists many member functions, which I want to use in reflection. 这个接口包含许多成员函数,我想在反射中使用它们。 According to my research on it, the problem is in this next line, which works fine until we don't need interface ie 根据我对它的研究,问题出在下一行,在我们不需要界面之前,它可以正常工作,即
Object objClass = (Object)Activator.CreateInstance(t);
Help me. 帮我。 I'm new to Generics. 我是泛型新手。 Thank you. 谢谢。

You can take a look at Castle DynamicProxy . 您可以看一下Castle DynamicProxy It lets you create proxies of your types on run-time which implement interfaces, base classes and many other things. 它使您可以在运行时创建类型的代理,以实现接口,基类和许多其他事情。

Check its documentation here . 在此处检查其文档。

There is no native solution for what you want as generic checking is made at compile time. 编译时进行泛型检查,因此您没有想要的本机解决方案。 At runtime you must use dynamic in your code or the previously recommended dll. 在运行时,您必须在代码或以前推荐的dll中使用dynamic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM