获取自定义属性值

法哈德

我需要获取自定义属性的值。我的要求几乎与此MSDN链接中给出的示例相似(如下所示)。

using System;
using System.Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized). 
public enum Animal {
    // Pets.
    Dog = 1,
    Cat,
    Bird,
}

// A custom attribute to allow a target to have a pet. 
public class AnimalTypeAttribute : Attribute {
    // The constructor is called when the attribute is set. 
    public AnimalTypeAttribute(Animal pet) {
        thePet = pet;
    }

    // Keep a variable internally ... 
    protected Animal thePet;

    // .. and show a copy to the outside world. 
    public Animal Pet {
        get { return thePet; }
        set { thePet = value; }
    }
}

// A test class where each method has its own pet. 
class AnimalTypeTestClass {
    [AnimalType(Animal.Dog)]
    public void DogMethod() {}

    [AnimalType(Animal.Cat)]
    public void CatMethod() {}

    [AnimalType(Animal.Bird)]
    public void BirdMethod() {}
}

class DemoClass {
    static void Main(string[] args) {
        AnimalTypeTestClass testClass = new AnimalTypeTestClass();
        Type type = testClass.GetType();
        // Iterate through all the methods of the class. 
        foreach(MethodInfo mInfo in type.GetMethods()) {
            // Iterate through all the Attributes for each method. 
            foreach (Attribute attr in
                Attribute.GetCustomAttributes(mInfo)) {
                // Check for the AnimalType attribute. 
                if (attr.GetType() == typeof(AnimalTypeAttribute))
                    Console.WriteLine(
                        "Method {0} has a pet {1} attribute.",
                        mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
            }

        }
    }
}
/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */

在上面的代码中,自定义属性被强制转换为其类型(AnimalTypeAttribute),然后获得其值。我有一个类似的场景,但是我没有自定义属性的引用,因此无法将其强制转换为获取其值。我可以获取属性节点,但是无法找到其最终值。在上面的示例中,属性为“ AnimalTypeAttribute”,其值为“ Dog”,“ Cat”,“ Bird”等。我能够找到属性节点“ AnimalTypeAttribute”,我现在需要的是值“狗”,“猫”等。有没有办法做到这一点?

其他信息(不确定是否会更改任何内容):我正在创建自定义FxCop规则,并且节点类型为Microsoft.FxCop.Sdk.AttributeNode

TIA。

法哈德

Microsoft.FxCop.Sdk.AttributeNode不能被转换到一个正常的System.AttributeSystem.Reflection.CustomAttributeData或其他任何东西,我试过了。最后,我可以使用以下函数获得所需的值:

public Microsoft.FxCop.Sdk.Expression GetPositionalArgument(int position)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Magento:获取自定义属性值,而无需加载整个产品

来自分类Dev

magento获取自定义属性标签

来自分类Dev

获取自定义属性的回发值

来自分类Dev

使用jQuery获取自定义属性

来自分类Dev

在Jquery中获取自定义属性值?

来自分类Dev

从Magento 1.9中的产品获取自定义属性

来自分类Dev

获取自定义的Magento属性值以进行计算

来自分类Dev

PHP使用DOM解析器获取自定义属性值

来自分类Dev

使用Javascript从按钮获取自定义属性

来自分类Dev

通过Runnable任务获取自定义线程的属性

来自分类Dev

如何获取自定义CSS属性列表

来自分类Dev

WooCommerce-获取自定义产品属性

来自分类Dev

angular2获取自定义属性的值

来自分类Dev

如何获取自定义属性的通用集合

来自分类Dev

从选择中获取自定义属性

来自分类Dev

从shortcode属性获取自定义分类法

来自分类Dev

在select2中使用Ajax时无法获取自定义属性值

来自分类Dev

获取自定义UITableViewCell的字符串属性值

来自分类Dev

Assembly.LoadFrom获取自定义属性

来自分类Dev

如何从Winrt的装配中获取自定义属性

来自分类Dev

从特定对象的属性/字段获取自定义属性

来自分类Dev

从C#事件获取自定义属性

来自分类Dev

使用jQuery获取自定义属性

来自分类Dev

使用LINQ to XML获取自定义属性值

来自分类Dev

在PHP中获取自定义属性值

来自分类Dev

获取自定义属性属性内的枚举类型

来自分类Dev

如何使用JQuery获取自定义HTML属性的值?

来自分类Dev

用于获取自定义属性值的 XPath 查询

来自分类Dev

如何使用 JAVASCRIPT 在 <option> 标签内获取自定义属性的值