如何从类型获取内部属性?

我有一堂课

[DefiniteAttribute]
public class Someclass
{
}

DefiniteAttribute的定义:

internal class DefiniteAttribute : Attribute
{
}

我想从课堂上获得属性 DefiniteAttribute 。但是我失败了,变量 definiteAttribute null

var definiteAttribute = (DefiniteAttribute) typeof(Someclass).getcustomAttribute<DefiniteAttribute>(false);

请告诉我,我该如何编写代码以便从类型中获取内部属性?

chenxinpao 回答:如何从类型获取内部属性?

原来,如果我将 inherit 参数设置为 true ,则内部属性将由方法 GetCustomAttribute

返回。
var definiteAttribute =  generatedType.GetCustomAttribute<DefiniteAttribute>(inherit: true);

尽管the documentation仅表示

inherit: true to inspect the ancestors of element; otherwise,false.
本文链接:https://www.f2er.com/3121966.html

大家都在问