MVC 3中的自定义属性 [英] Custom attributes in MVC 3

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

问题描述

希望在MVC 3中创建自定义属性.并且还想知道如何从控制器调用该属性.帮我做到这一点.

感谢

wanted to create custom attribute in MVC 3 . and also want to know how to invoke the attribute from controllers. help me to do this.

thanks

推荐答案

这个问题与MVC并没有真正的关系.您不会调用"属性-没什么好想的.这些属性作为附加元数据归因"于程序集,模块,类型,成员和参数.当您执行运行时的任何部分时,它们已经存在.您可以做所有的工作来检查上述编程实体的声明是否具有某种类型的属性.如果找到了某种类型的属性,则可以读取其属性.这可以使用某些名为GetCustomAttributes:
的方法来完成. http://msdn.microsoft.com/en-us/library/system.attribute. getcustomattributes.aspx [ ^ ].

还有更多方法,但我不想显示它们与大多数反射类型一起使用:AssemblyTypeMethodInfoConstructorInfoMemberInfoFieldInfoPropertyInfoParameterInfo等.最好根据需要自己找到合适的属性相关方法.

—SA
The question is not really related to MVC. You don''t "invoke" an attribute — there is no suck think. The attributes are "attributed" the the assemblies, modules, types, members and parameters as additional meta-data. When you execute any portion of your run time, they are already there. All you can do it to examine if the declarations of the programming entities mentioned above have some type of attributes; and if an attribute of certain type is found, you can read its properties. This can be done using some methods named GetCustomAttributes:
http://msdn.microsoft.com/en-us/library/system.attribute.getcustomattributes.aspx[^].

There are more methods, but I don''t want to show them as they comes with most Reflection types: Assembly, Type, MethodInfo, ConstructorInfo, MemberInfo, FieldInfo, PropertyInfo, ParameterInfo and the like. You would be better off finding appropriate attribute-related methods by yourself, as you need them.

—SA


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

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