gpt4 book ai didi

c# - dll 中的可选外部接口(interface)实现

转载 作者:行者123 更新时间:2023-11-30 23:30:11 28 4
gpt4 key购买 nike

我正在创建一些集成库,它将接受各种接口(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,我希望它在没有核心的环境中运行,它是 IExample 和 IModule 接口(interface)。 (我想要一个 .dll,而不是两个用于带和不带这些接口(interface)的环境)。

有没有办法在 C# 中实现它?如果不是(出于好奇)是否有一种语言可以实现?

最佳答案

不,你不能。 CLR 需要带有接口(interface)的程序集,因为它需要知道它们的外观。但是,如果您担心运送两个程序集(为什么?),您可以将它们与 ILMerge 合并.但是,这带来了其他问题和考虑因素。您是否也打算将另一个模块与核心合并?这可能会导致运行时异常。

我不知道还有另一种 .NET 语言可以满足您的需求。事实上,我怀疑是否有任何其他语言支持未随实现代码一起提供的接口(interface)。

关于c# - dll 中的可选外部接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35032672/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com