<bdo id='3JAY3'></bdo><ul id='3JAY3'></ul>

    <small id='3JAY3'></small><noframes id='3JAY3'>

    1. <legend id='3JAY3'><style id='3JAY3'><dir id='3JAY3'><q id='3JAY3'></q></dir></style></legend>

    2. <tfoot id='3JAY3'></tfoot>

      <i id='3JAY3'><tr id='3JAY3'><dt id='3JAY3'><q id='3JAY3'><span id='3JAY3'><b id='3JAY3'><form id='3JAY3'><ins id='3JAY3'></ins><ul id='3JAY3'></ul><sub id='3JAY3'></sub></form><legend id='3JAY3'></legend><bdo id='3JAY3'><pre id='3JAY3'><center id='3JAY3'></center></pre></bdo></b><th id='3JAY3'></th></span></q></dt></tr></i><div id='3JAY3'><tfoot id='3JAY3'></tfoot><dl id='3JAY3'><fieldset id='3JAY3'></fieldset></dl></div>
    3. 任何人都知道快速获取枚举值的自定义属性的方法吗?

      Anyone know a quick way to get to custom attributes on an enum value?(任何人都知道快速获取枚举值的自定义属性的方法吗?)

    4. <small id='xvese'></small><noframes id='xvese'>

        <bdo id='xvese'></bdo><ul id='xvese'></ul>
        1. <legend id='xvese'><style id='xvese'><dir id='xvese'><q id='xvese'></q></dir></style></legend>

          • <tfoot id='xvese'></tfoot>
                  <tbody id='xvese'></tbody>

                <i id='xvese'><tr id='xvese'><dt id='xvese'><q id='xvese'><span id='xvese'><b id='xvese'><form id='xvese'><ins id='xvese'></ins><ul id='xvese'></ul><sub id='xvese'></sub></form><legend id='xvese'></legend><bdo id='xvese'><pre id='xvese'><center id='xvese'></center></pre></bdo></b><th id='xvese'></th></span></q></dt></tr></i><div id='xvese'><tfoot id='xvese'></tfoot><dl id='xvese'><fieldset id='xvese'></fieldset></dl></div>
                本文介绍了任何人都知道快速获取枚举值的自定义属性的方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这可能最好用一个例子来说明.我有一个带有属性的枚举:

                This is probably best shown with an example. I have an enum with attributes:

                public enum MyEnum {
                
                    [CustomInfo("This is a custom attrib")]
                    None = 0,
                
                    [CustomInfo("This is another attrib")]
                    ValueA,
                
                    [CustomInfo("This has an extra flag", AllowSomething = true)]
                    ValueB,
                }
                

                我想从一个实例中获取这些属性:

                I want to get to those attributes from an instance:

                public CustomInfoAttribute GetInfo( MyEnum enumInput ) {
                
                    Type typeOfEnum = enumInput.GetType(); //this will be typeof( MyEnum )
                
                    //here is the problem, GetField takes a string
                    // the .ToString() on enums is very slow
                    FieldInfo fi = typeOfEnum.GetField( enumInput.ToString() );
                
                    //get the attribute from the field
                    return fi.GetCustomAttributes( typeof( CustomInfoAttribute  ), false ).
                        FirstOrDefault()        //Linq method to get first or null
                        as CustomInfoAttribute; //use as operator to convert
                }
                

                由于这是使用反射,我预计会有些慢,但是当我已经有枚举值的实例时,将枚举值转换为字符串(反映名称)似乎很麻烦.

                As this is using reflection I expect some slowness, but it seems messy to convert the enum value to a string (which reflects the name) when I already have an instance of it.

                谁有更好的办法?

                推荐答案

                这可能是最简单的方法了.

                This is probably the easiest way.

                一种更快的方法是使用动态方法和 ILGenerator 静态发出 IL 代码.虽然我只将它用于 GetPropertyInfo,但看不出为什么你也不能发出 CustomAttributeInfo.

                A quicker way would be to Statically Emit the IL code using Dynamic Method and ILGenerator. Although I've only used this to GetPropertyInfo, but can't see why you couldn't emit CustomAttributeInfo as well.

                例如从属性发出 getter 的代码

                For example code to emit a getter from a property

                public delegate object FastPropertyGetHandler(object target);    
                
                private static void EmitBoxIfNeeded(ILGenerator ilGenerator, System.Type type)
                {
                    if (type.IsValueType)
                    {
                        ilGenerator.Emit(OpCodes.Box, type);
                    }
                }
                
                public static FastPropertyGetHandler GetPropertyGetter(PropertyInfo propInfo)
                {
                    // generates a dynamic method to generate a FastPropertyGetHandler delegate
                    DynamicMethod dynamicMethod =
                        new DynamicMethod(
                            string.Empty, 
                            typeof (object), 
                            new Type[] { typeof (object) },
                            propInfo.DeclaringType.Module);
                
                    ILGenerator ilGenerator = dynamicMethod.GetILGenerator();
                    // loads the object into the stack
                    ilGenerator.Emit(OpCodes.Ldarg_0);
                    // calls the getter
                    ilGenerator.EmitCall(OpCodes.Callvirt, propInfo.GetGetMethod(), null);
                    // creates code for handling the return value
                    EmitBoxIfNeeded(ilGenerator, propInfo.PropertyType);
                    // returns the value to the caller
                    ilGenerator.Emit(OpCodes.Ret);
                    // converts the DynamicMethod to a FastPropertyGetHandler delegate
                    // to get the property
                    FastPropertyGetHandler getter =
                        (FastPropertyGetHandler) 
                        dynamicMethod.CreateDelegate(typeof(FastPropertyGetHandler));
                
                
                    return getter;
                }
                

                这篇关于任何人都知道快速获取枚举值的自定义属性的方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                How can I force the PropertyGrid to show a custom dialog for a specific property?(如何强制 PropertyGrid 显示特定属性的自定义对话框?)
                Associate attribute with code generated property in .net(将属性与 .net 中的代码生成属性相关联)

                1. <tfoot id='XX4K8'></tfoot>
                  <legend id='XX4K8'><style id='XX4K8'><dir id='XX4K8'><q id='XX4K8'></q></dir></style></legend>

                      <bdo id='XX4K8'></bdo><ul id='XX4K8'></ul>
                    • <small id='XX4K8'></small><noframes id='XX4K8'>

                      1. <i id='XX4K8'><tr id='XX4K8'><dt id='XX4K8'><q id='XX4K8'><span id='XX4K8'><b id='XX4K8'><form id='XX4K8'><ins id='XX4K8'></ins><ul id='XX4K8'></ul><sub id='XX4K8'></sub></form><legend id='XX4K8'></legend><bdo id='XX4K8'><pre id='XX4K8'><center id='XX4K8'></center></pre></bdo></b><th id='XX4K8'></th></span></q></dt></tr></i><div id='XX4K8'><tfoot id='XX4K8'></tfoot><dl id='XX4K8'><fieldset id='XX4K8'></fieldset></dl></div>
                            <tbody id='XX4K8'></tbody>