简体   繁体   English

使用Reflection c#实现接口

[英]Implementing Interface using Reflection c#

I have a requirement where I need to implement an interface in my project which is present in a third part DLL. 我有一个要求,我需要在我的项目中实现一个接口,它存在于第三部分DLL中。 I'm loading this DLL using reflection. 我正在使用反射加载此DLL。 Is it Possible? 可能吗?

class MyClass : I3rdPartyInterface
{
    //implementing interface   
}

Here, the I3rdPartyInterface is the interface present in my 3rd Party DLL. 这里, I3rdPartyInterface是我的第三方DLL中的接口。 This DLL could, or could not, be present on Client machine my software product is installed. 此DLL可能或可能不会出现在我的软件产品安装的客户端计算机上。 If it is present, then i should implement the interface methods. 如果它存在,那么我应该实现接口方法。

Your best option is to write a new project that references that dll and implements the interface - and load that in runtime. 您最好的选择是编写一个引用该DLL并实现该接口的新项目 - 并在运行时加载该项目。

The new dll "translates" between the external dll types and your application's types - that way there is no direct dependency between your app and the external dll. 新的dll在外部dll类型和应用程序类型之间“转换” - 这样你的应用程序和外部dll之间就没有直接的依赖关系。

You can generate and compile code at runtime but for this scenario it's just simpler to create a wrapper/translator dll 您可以在运行时生成和编译代码,但是对于这种情况,创建包装器/转换器dll更简单

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

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