委托自定义属性 [英] delegate in custom attribute

查看:68
本文介绍了委托自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义属性,我用于枚举中的字段。


我想在这些事情中传递一个代表。


所以我想做这样的事情


[MyCustomAttrb(字符串名称,字符串desc,int值,委托clickEvent)]


这可能吗?我没有运气。

解决方案

Nathan Laff< re ****** @ hotmail.comwrote:
< blockquote class =post_quotes>
我有一个自定义属性,我用于枚举中的字段。


我想在这些事情中传递一个代表。 />

所以我想做这样的事情


[MyCustomAttrb(字符串名称,字符串desc,int值,委托clickEvent)]


这可能吗?我没有运气。



编号属性参数受规范限制:


< quote>

属性类的位置和命名参数的类型是

仅限于属性参数类型,它们是:


*以下类型之一:bool ,byte,char,double,float,int,

long,short,string。

*类型对象。

*类型系统.Type。

*枚举类型,前提是它具有公共可访问性,并且嵌套的类型

(如果有的话)也具有公共可访问性。

*以上类型的单维数组。

< / quote>


-

Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet 博客: http:// www。 msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


这并不意味着你不能揭露一个会占用
$ b的方法/事件$ b委托但是。您可以通过反射获取属性,然后将

您的委托传递给方法/事件。


然而,这有点无意义,因为你不能在属性和分配给它的内容之间建立关联

(来自属性,

)。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP *********************** @ msnews.microsoft .co m ...


Nathan Laff< re ****** @ hotmail.comwrote:


>我有一个自定义属性,我用于枚举中的字段。

我想在这些事情中传递一个代表。

所以我想要做这样的事情

[MyCustomAttrb(字符串名称,字符串desc,int值,委托clickEvent)]

这可能吗?我没有运气。



编号属性参数受规范限制:


< quote>

属性类的位置和命名参数的类型是

仅限于属性参数类型,它们是:


*以下类型之一:bool ,byte,char,double,float,int,

long,short,string。

*类型对象。

*类型系统.Type。

*枚举类型,前提是它具有公共可访问性,并且嵌套的类型

(如果有的话)也具有公共可访问性。

*以上类型的单维数组。

< / quote>


-

Jon Skeet - < sk *** @ pobox.com>
http:// www.pobox.com/~skeet 博客: http:// www。 msmvps.com/jon.skeet

如果重新请小组,请不要给我发邮件



没有。属性参数受规范限制:


>

< quote>

位置类型属性类的命名参数是

仅限于属性参数类型,它们是:


*以下类型之一:bool,byte,char ,double,float,int,

long,short,string。

*类型对象。

*类型System.Type。

*枚举类型,只要它具有公共可访问性,并且嵌套的类型

(如果有的话)也具有公共可访问性。

*上述类型的一维数组。

< / quote>



这不可能完全正确,因为它在

属性构造函数中使用Delegate类型构建?我似乎无法在其中得到任何工作

:P


I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i''m having no luck.

解决方案

Nathan Laff <re******@hotmail.comwrote:

I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i''m having no luck.

No. Attribute parameters are limited by the spec:

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


It doesn''t mean that you can''t expose a method/event which will take the
delegate though. You can get the attribute through reflection and then pass
your delegate to the method/event.

However, it''s kind of pointless, since you can''t make a correlation
between the attribute and what it''s assigned to (from within the attribute,
that is).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP***********************@msnews.microsoft.co m...

Nathan Laff <re******@hotmail.comwrote:

>I have a custom attribute which i use for fields in an enum.

I want to pass around a delegate in these things.

so i want to do something like this

[MyCustomAttrb(string name, string desc, int value, delegate clickEvent)]

Is this possible? i''m having no luck.


No. Attribute parameters are limited by the spec:

<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



No. Attribute parameters are limited by the spec:

>
<quote>
The types of positional and named parameters for an attribute class are
limited to the attribute parameter types, which are:

* One of the following types: bool, byte, char, double, float, int,
long, short, string.
* The type object.
* The type System.Type.
* An enum type, provided it has public accessibility and the types
in which it is nested (if any) also have public accessibility.
* Single-dimensional arrays of the above types.
</quote>

That can''t be entirely true as it builds with a Delegate type in the
attribute contructor? I just can''t seem to get anything to work right in it
:P


这篇关于委托自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆