小编Ger*_*ine的帖子

Autofac基于类属性/元数据解析

我想根据类属性注册和解析动态加载的类型.我的代码如下:

自定义类属性:

[MetadataAttribute]
public class FooIdentifier : Attribute
{
    public string Identifier { get; private set; }

    public FooIdentifier(string identifier)
    {
        this.Identifier = identifier;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的抽象基类

public abstract class Base
{
    public abstract bool Execute(object param);

    public bool Run(object param = null)
    {
        //...
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的实现类型

[FooIdentifier("230")]
public class Child1 : Base
{
    public override bool Execute(object param)
    {
        throw new NotImplementedException();
    }
}

[FooIdentifier("250")]
public class Child2 : Base
{
    public override bool Execute(object param) …
Run Code Online (Sandbox Code Playgroud)

attributes autofac

2
推荐指数
1
解决办法
2708
查看次数

标签 统计

attributes ×1

autofac ×1