[英]How do I give a method as an attribute argument?
I want to apply an attribute which accepts a delegate as an argument, but I can't find the syntax to do it. 我想应用一个接受委托作为参数的属性,但我找不到这样做的语法。
For example, to pass a class, you have to use typeof: 例如,要传递一个类,您必须使用typeof:
[SomeAttribute(typeof(SomeClass))]
What is the syntax for a delegate (I am trying to pass a static method)? 委托的语法是什么(我试图传递一个静态方法)?
[SomeAttribute(??? SomeStaticMethod ???]
This is beyond the capacity of the Meta Data. 这超出了元数据的能力。
You could pass the method as a string and also specify the class if you just want to call a static method.. 您可以将该方法作为字符串传递,如果您只想调用静态方法,也可以指定该类。
[SomeAttribute(typeof(SomeClass), @"SomeStaticMethod")]
Naturally you would have to invoke it through reflection, but since you are looking up the custom attributes anyway this probably isn't a big deviation. 当然你必须通过反射调用它,但是因为你正在查找自定义属性,所以这可能不是一个很大的偏差。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.