我正在创建一些集成库,它将接受各种接口(interface)实现以使其运行。
我有一个设置:
图书馆
interface IModule {
void Init();
}
interface IExample : IModule {
void DoSomething();
}
class Core {
IExample Example {get; private set;}
}
核心类正在查找 IModule 类型的每个属性,并根据外部配置为每个类型分配一个实现属性类型接口(interface)的实例对象。
那么我就有了一个满足Core要求的模块。
class MyExampleModule : IExample {
public void Init() { }
public void DoSomething() { }
public void ExampleFeature1() { }
public void ExampleFeature2() { }
}
我想关闭 .dll 中的模块 MyExampleModule,并且希望它在没有 Core 的环境中运行,并且它是 IExample 和 IModule 接口(interface)。 (我想要一个 .dll,而不是两个用于有和没有这些接口(interface)的环境)。
有没有办法在 C# 中实现这一点?如果没有(只是出于好奇的原因)是否有一种语言可以实现?
最佳答案
不,你不能。 CLR 需要带有接口(interface)的程序集,因为它需要知道它们是什么样子。但是,如果您担心运送两个程序集(为什么?),您可以将它们与 ILMerge 合并。 。但这会带来其他问题和考虑因素。您是否也打算将另一个模块与核心合并?这可能会导致运行时异常。
我不知道还有其他 .NET 语言可以实现您想要的功能。事实上,我怀疑是否有任何其他语言支持未随实现代码一起提供的接口(interface)。
关于c# - dll 中可选的外部接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032672/