让我们在 ctr 中有一个带有 int 参数的属性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class EntityBindingAttribute : Attribute
{
public int I { get; set; }
public EntityBindingAttribute(int i)
{
I = i;
}
}
如何使用代码访问此值?
请您参考如下方法:
您将使用 Attribute.GetCustomAttributes Method , 具有与已设置属性的对象匹配的重载。
例如,如果属性是在方法上设置的,例如:
MethodInfo mInfo = myClass.GetMethod("MyMethod");
foreach(Attribute attr in Attribute.GetCustomAttributes(mInfo))
{
if (attr.GetType() == typeof(EntityBindingAttribute))
{
int i = ((EntityBindingAttribute)attr).I);
}
}
