简体   繁体   English

如何将方法作为属性参数?

[英]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.

相关问题 如何在C#中序列化期间为数组赋予属性? - How do I give an array an attribute during serialization in C#? 如何找到带有params参数的方法的用法,使得参数不为空? - How do I find usages of a method with a params argument such that the argument is not empty? 如何为在单击事件内定义的对象提供方法? - How do I give a method to an object that is defined inside of a click event? 如何在泛型方法中获取类型参数的名称? - How do I get the name of a type argument in a generic method? 如何为此方法参数传递文本框值? - How do I pass textbox values for this method argument? 如何为每个播放器对象赋予一个playerID属性,并使它们根据不同的输入移动? - How do I give my player object each a playerID attribute and make them move based on different inputs? 如何修复此方法以给我 8 个单独的总数形成一个 3 维数组而不是一个 - How do I fix this method to give me 8 individual totals form a 3 dimensional array instead of a 我怎样才能给 Dispatcher.Invoke 一个论点? - How can I give an argument to Dispatcher.Invoke? 如何解决尝试调用包含 KeyEventArgs e 作为参数的方法时出现的错误 - How do I fix the error I have when trying to call the method that contain KeyEventArgs e as argument 如何给SqlConnection一个字符串作为AttachDbFilename? - How do I give an SqlConnection a string as the AttachDbFilename?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM