【发布时间】:2020-08-03 03:21:01
【问题描述】:
我想用自定义属性来装饰一些函数。这些函数不会返回特定类型,实际上可以返回任何类型。
现在这个函数将调用任意数量的其他函数,然后在操作中的某个时刻我想知道“调用堆栈中的文本/值最后一个自定义属性是什么”。
我该怎么做?
例子:
[CustomAttribute("Hello World...")]
public void SomeFunction
{
return Func1("myparam")
}
public void Func1(string xx)
{
return Func2(xx)
}
public string Func2(string yy)
{
//I would like to know what is the value\text of the attribute "CustomAttribute".
//If there are multiples in the call stack, return the last one (which might be in another class and project as Func2)
}
【问题讨论】:
-
在运行时处理自定义属性通常涉及
System.ReflectionAPI。我建议首先编写代码来获取您知道的特定硬编码方法的CustomAttribute值。然后,您可以修改该代码以搜索System.Diagnostics.StackTrace类的实例。
标签: c# custom-attributes