我在这里创建了一个名为AAtribute的自定义属性,例如一个名为B的类,其中一个或多个方法使用该属性。是否可以将保存属性的方法的MethodInfo(在这种情况下是BMethod1)作为属性中的一个,而不必遍历整个程序集并查看所有已定义的方法的属性?并且它们是其他AttributeTargets(参数/类型/属性/ ...)的模拟方式吗?我不想要一个使用该类型的Attribute的所有方法的数组,但只需要具有此Attirbute对象的方法。我想用它来为方法添加额外的约束(返回类型,参数,名称,其他Attribute-usage,...)。快速访问C#中保存属性的类型/方法/ ...
[AttributeUsage(AttributeTargets.Method)]
public class AAtribute : Attribute {
//some fields and properties
public AAtribute() {//perhaps with some parameters
//some operations
MethodInfo mi;//acces to the MethodInfo with this Attribute
//as an Attribute (the question)
//some operations with the MethodInfo
}
//some methods
}
public class B {
//some fields, properties and constructors
[A]
public void BMethod1() {
//some operations
}
//other methods
}
好问题,我也想知道答案... – 2009-08-28 10:06:41