C# 什么';这是让人们在我';我装了

C# 什么';这是让人们在我';我装了,c#,.net,reflection,add-in,C#,.net,Reflection,Add In,我有一个类库(CoreLib),它公开了一系列功能,包括开箱即用和通过附加组件 与插件交互的接口非常通用(故意),每个插件都可以返回描述其公开的属性的元数据。插件的基类如下所示: public abstract class AddOnBase { public abstract IList<PropertyDescriptor> GetPropertyDescriptors(); public void SetPropertyValue(Guid propertyId, o

我有一个类库(CoreLib),它公开了一系列功能,包括开箱即用和通过附加组件

与插件交互的接口非常通用(故意),每个插件都可以返回描述其公开的属性的元数据。插件的基类如下所示:

public abstract class AddOnBase 
{
  public abstract IList<PropertyDescriptor> GetPropertyDescriptors();
  public void SetPropertyValue(Guid propertyId, object value);
  public object GetPropertyValue(Guid propertyId);
}
这很好,但是开发人员需要查找所有这些guid,这使得所有东西都非常笨拙

我更希望开发人员可以添加对RecipeAddOn的引用,并编写如下内容:

Guid recipeAddOnId = RecipeAddOn.Id;

AddOn addon = coreLib.GetService(IAddOnService).GetAddOnInstance(recipeAddOnId);

// Cast the instance returned by CoreLib to a RecipeAddOn
RecipeAddOn recipe = (RecipeAddOn)addon;

// Set some properties in the recipe addon.
recipe.Title = "Cupcakes";
recipe.Category = "Baked Goods";

// Do something with the addon.
coreLib.GetService(IAddOnService).ProcessAddOn(recipe);
在这里,他们不仅不必使用guid来识别属性,而且还可以使用更具体的类并使用intellisense来帮助他们进行开发


我目前的解决方案是让开发人员同时引用CoreLib和RecipeAddOn,后者的
CopyLocal
设置为
false
。然而,有人建议,这可能导致DLL地狱(请参阅),因此我想问一下这样做的正确方法是什么。

我的扩展设置总是这样。它起源于C和Pascal时代,每个人都发布一个头文件或Pascal单元,带有接口和一个要链接的lib/obj文件

  • 客户
  • 接口的
  • 实现DLL

客户端引用包含
irecipeadon
接口的接口dll。在运行时,客户端还加载实现dll,创建一个
RecipeAddon
concrete类的实例,并将其强制转换到
IRecipeAddon
接口。开发人员获得intellisense,并且加载项实现不会与客户端代码混合。

您应该关注动态。这正是您的任务。是的-这是一个选项,但是开发人员将不会获得intellisense,这将是很好的。这在一个基本场景中运行良好,但在我的情况下,客户端正在加载一个库,而该库就是加载addonIt的内容。这并不重要。需要外接程序实例的代码总是使用接口,无论外接程序是从库中加载的,还是动态创建的,还是从DI库中创建的。它只是引用接口dll并请求IAddOn实例。我已经使用这种技术来加载设备通信库。在客户端中,我使用了
IDevice device=ServiceManager.GetService().GetDeviceByName(“测试设备”)
Guid recipeAddOnId = RecipeAddOn.Id;

AddOn addon = coreLib.GetService(IAddOnService).GetAddOnInstance(recipeAddOnId);

// Cast the instance returned by CoreLib to a RecipeAddOn
RecipeAddOn recipe = (RecipeAddOn)addon;

// Set some properties in the recipe addon.
recipe.Title = "Cupcakes";
recipe.Category = "Baked Goods";

// Do something with the addon.
coreLib.GetService(IAddOnService).ProcessAddOn(recipe);