相关疑难解决方法(0)

如何获取枚举的自定义属性值?

我有一个枚举,每个成员都有一个自定义属性应用于它.如何检索存储在每个属性中的值?

现在我这样做:

var attributes = typeof ( EffectType ).GetCustomAttributes ( false );
foreach ( object attribute in attributes )
{
    GPUShaderAttribute attr = ( GPUShaderAttribute ) attribute;
    if ( attr != null )
        return attr.GPUShader;
}
return 0;
Run Code Online (Sandbox Code Playgroud)

另一个问题是,如果找不到,我应该返回什么?0是可以转换为任何枚举,对吧?这就是我回归的原因.

忘记提及,上面的代码为每个枚举成员返回0.

.net c# reflection custom-attributes

40
推荐指数
3
解决办法
4万
查看次数

标签 统计

.net ×1

c# ×1

custom-attributes ×1

reflection ×1