我正在尝试定义一个具有类级别的CustomAttribute,并且我希望这个属性被传播到类的所有属性。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class MyCustomAttribute : Attribute
{
}
[MyCustom]
public class MyClass
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
}我希望属性A、B和C也有MyCustomAttribute赋值。我知道我可以为每个属性定义属性。但是我想在类级别定义属性。
有可能吗?
发布于 2018-09-27 21:54:15
这是不可能的,但根据你如何使用自定义属性,你可以让它检查定义属性的类,即。
public static bool IsDefinedOnPropertyOrClass(PropertyInfo info, Type attributeType)
{
return info.IsDefined(attributeType) || info.DeclaringType.IsDefined(attributeType);
}https://stackoverflow.com/questions/52538567
复制相似问题