首页 > 解决方案 > C# 自定义属性传递给被调用的方法

问题描述

有没有办法获得沿流程添加的自定义属性?我正在将自定义属性添加到根据流程调用的不同类中的不同方法。我试过 MethodBase.GetCurrentMethod() 但这(正如它所说)只是当前调用的方法。使用 new StackFrame(n) 也不起作用,因为“n”可以经常更改。谷歌搜索了我的问题,但没有找到任何提示。

public class CustomAttribute1 : Attribute 
{
    public bool Property { get; }

    public CustomAttribute1(bool property) => Property = property;
}

public class CustomAttribute2 : Attribute
{
    public string Property { get; }

    public CustomAttribute2(string property) => Property = property;
}


public class GeneralService 
{
    protected void Resolve() 
    {
        // Here I want to access custom attributes combined 
    }
}

public class SpecifiedService : GeneralService
{
    [CustomAttribute1(true)]
    public void SendRequest() 
    {
        Resolve();
    }
}

public class Worker
{
    [CustomAttribute2("init")]
    public void Init()
    {
        var service = new SpecifiedService();
        service.SendRequest();
    }
}

标签: c#.net

解决方案


推荐阅读