c# - 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();
}
}
解决方案
推荐阅读
- cmake - How to print without newline in CMake?
- javascript - 根据我的下拉选择在我的文本框中填充 Db 值
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 1039 中的字节 0x92:序数不在范围内(128)
- webpack - Webpack 供应商块太大
- ms-access - 访问数据库查询以将由“,”分隔的列拆分为多行
- scala - 如何以纯 FP 方式设计有状态的 Actor
- html - 在 asp.net 中的相同 div 中迭代循环
- apache-kafka-streams - Materialized<> 类型中的 withValueSerde() 方法不适用
- vba - Application.Worksheetfunction.Max 不返回数组中的最大数
- python-2.7 - Python:ZeroMQ 是否可以设置一个回调,以便在收到消息后调用?