我有一个枚举,每个成员都有一个自定义属性应用于它.如何检索存储在每个属性中的值?
现在我这样做:
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.