gpt4 book ai didi

c# - MEF 无属性方法 : lazy initialization by condition

转载 作者:行者123 更新时间:2023-12-02 05:17:20 27 4
gpt4 key购买 nike

我之前的帖子包含尝试使用无属性(基于约定)的方法来配置 MEF:MEF 2: import many .
但它包含按条件(特定名称、版本)延迟初始化插件所需的类 PluginMetadataAttribute 中的导出元数据属性用法。
如何摆脱对 ExportAttribute 的依赖?

最佳答案

我找到了三个解决方案。
解决方案一(使用类常量字段,较差的解决方案):

public class Plugin1 : IPlugin
{
public const string Name = "Plugin1";
public const string Version = "1.0.0.0";

public void Run()
{
Console.WriteLine("Plugin1 runed");
}
}
// ...
var builder = new RegistrationBuilder();
builder
.ForTypesDerivedFrom<IPlugin>()
.Export<IPlugin>(exportBuilder => {
exportBuilder.AddMetadata("Name", t => t.GetField("Name").GetRawConstantValue());
exportBuilder.AddMetadata("Version", t => t.GetField("Version").GetRawConstantValue());
});

解决方案2(使用类属性,较差的解决方案):

public interface IPluginMetadata
{
string Name { get; }
string Version { get; }
}

public interface IPlugin : IPluginMetadata
{
void Run();
}

public class Plugin1 : IPlugin
{
public string Name { get { return "Plugin 1"; } }
public string Version { get { return "1.0.0.0"; } }

public void Run()
{
Console.WriteLine("Plugin1 runed");
}
}

并通过以下描述的方法获取属性值:https://stackoverflow.com/a/11162876/1986524

解决方案 3(使用属性,更好但不是很满意):

using System;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Registration;
using System.Reflection;

namespace MEF2
{
public interface IPluginMetadata
{
string Name { get; }
string Version { get; }
}

public interface IPlugin
{
void Run();
}

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class PluginMetadataAttribute : Attribute, IPluginMetadata
{
public string Name { get; set; }
public string Version { get; set; }

public PluginMetadataAttribute(string name, string version)
{
Name = name;
Version = version;
}
}

[PluginMetadata("Plugin1", "1.0.0.0")]
public class Plugin1 : IPlugin
{
public void Run()
{
Console.WriteLine("Plugin1 runed");
}
}

[PluginMetadata("Plugin2", "2.0.0.0")]
public class Plugin2 : IPlugin
{
public void Run()
{
Console.WriteLine("Plugin2 runed");
}
}

class Program
{
static void Main(string[] args)
{
var builder = new RegistrationBuilder();
builder
.ForTypesDerivedFrom<IPlugin>()
.Export<IPlugin>(exportBuilder => {
exportBuilder.AddMetadata("Name", t => t.GetCustomAttribute<PluginMetadataAttribute>().Name);
exportBuilder.AddMetadata("Version", t => t.GetCustomAttribute<PluginMetadataAttribute>().Version);
});

var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly(), builder);

using (var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection)) {
var plugins = container.GetExports<IPlugin, IPluginMetadata>();

foreach (var plugin in plugins) {
Console.WriteLine("{0}, {1}", plugin.Metadata.Name, plugin.Metadata.Version);
plugin.Value.Run();
}
}
}
}
}

解决方案 3 包含此代码中的问题:

.Export<IPlugin>(exportBuilder => {
exportBuilder.AddMetadata("Name", t => t.GetCustomAttribute<PluginMetadataAttribute>().Name);
exportBuilder.AddMetadata("Version", t => t.GetCustomAttribute<PluginMetadataAttribute>().Version);
})

问题:

  1. 无法在缺少元数据的情况下取消添加元数据
  2. 重复代码 t.GetCustomAttribute<PluginMetadataAttribute>()
  3. Export<>不提供过滤器

如果有人知道其他解决方案,请写信。

关于c# - MEF 无属性方法 : lazy initialization by condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14455768/

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