Reflection and autogenerated types

前端 未结 2 1011
天命终不由人
天命终不由人 2021-01-15 05:05

I have a class with a single method that uses a \"yield\" return statement. A nested type is automatically created. Using reflection with binding flags set to BindingF

2条回答
  •  执笔经年
    2021-01-15 05:37

    You can test if the type has the [CompilerGenerated] attribute:

    if (type.GetCustomAttribute(typeof(CompilerGeneratedAttribute), true) != null)
    {
        ...
    }
    

    Alternatively, you can check if the name contains characters that wouldn't be valid in user code.

提交回复
热议问题