2011-10-04 60 views
2

我在寻找我怎么能知道属性定义里面如果I类应用属性,还有一个属性
例子:属性和类

[My1Attribute] 
public class MyClass 
{ 
    [My2Attribute] 
    int aux{get;set;} 

}   

internal sealed class My1Attribute : Attribute 
{ 
    public My1Attribute 
    { 
      // How can I Know if 'MyClass' has My2Attribute applied ??? 
    } 
} 

回答

2

的属性本身不会知道的它附加的类。您将需要使用其他服务/帮助器功能/无论将它们配对。

但是,您可能会发现下面的有用:

public static bool HasAttribute<T, TAttribute>() where TAttribute : Attribute 
{ 
    return typeof (T).GetCustomAttributes(typeof (TAttribute), true).Any(); 
} 

编辑:有关成员字段找到属性

/// <summary> 
/// Returns all the (accessible) fields or properties that for a given type that 
/// have the "T" attribute declared on them. 
/// </summary> 
/// <param name="type">Type object to search</param> 
/// <returns>List of matching members</returns> 
public static List<MemberInfo> FindMembers<T>(Type type) where T : Attribute 
{ 
    return FindMembers<T>(type, MemberTypes.Field | MemberTypes.Property); 
} 

/// <summary> 
/// Returns all the (accessible) fields or properties that for a given type that 
/// have the "T" attribute declared on them. 
/// </summary> 
/// <param name="type">Type object to search</param> 
/// <returns>List of matching members</returns> 
public static List<MemberInfo> FindMembers<T>(Type type, MemberTypes memberTypesFlags) where T : Attribute 
{ 
    const BindingFlags FieldBindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; 

    List<MemberInfo> members = new List<MemberInfo>(); 
    members.AddRange(type.FindMembers(
          memberTypesFlags, 
          FieldBindingFlags, 
          HasAttribute<T>, // Use delegate from below... 
          null)); // This arg is ignored by the delegate anyway... 

    return members; 
} 

public static bool HasAttribute<T>(MemberInfo mi) where T : Attribute 
{ 
    return GetAttribute<T>(mi) != null; 
} 

public static bool HasAttribute<T>(MemberInfo mi, object o) where T : Attribute 
{ 
    return GetAttribute<T>(mi) != null; 
} 
+0

但我不能在属性 – Tekno

+0

中使用这个,你可以把它作为一个静态函数,但如果我是你,我会用它作为扩展方法或在一个静态辅助类中。 – Reddog

+0

非常感谢!但我怎么能知道该属性绑定在属性本身内部的类来应用此!! !! !!?!例如,我有一个“OrderableAttribute”和一个“DefaultOrderAttribute”。如果我用“OrderableAttribute”定义一个类,我需要检查DefaultOrderAttribute是否也设置了 – Tekno

1

在这种情况下,您将n eed去定义你的规则,关于你如何确定你要检查的成员。在你的榜样,您使用的归因装饰上的属性,所以给你有TypeMyClass(如typeof(MyClass))的情况下,你可以抓住的属性:

var property = type.GetProperty("aux", BindingFlags.Instance | BindingFlags.NonPublic); 
if (property.IsDefined(typeof(My1Attribute))) 
{ 
    // Property has the attribute. 
} 

(这实际上是假设你想要抓住那个非公开的实例属性,如果不调整你的BindingFlags)。

如果你确实想使用的属性:

var attib = property.GetCustomAttributes(typeof(My1Attribute), false)[0]; 
// Do something with the attribute instance. 
0

我猜你在一般的意思是看看是否有任何MyAttribute1类有My2Attribute(而不是专门MyClass的)。我能想到的唯一方法是从反射中获取所有类的列表,并通过遍历它们来检查哪些类具有Attribute1,然后检查它们是否具有Attribute2。

我不认为你可以做任何聪明的事情,如自动检索具有当前属性的类的列表。

+0

我想要做类似于你所说的事情。想象一下,我有一个“Orderable”属性,我需要一个“DefaultOrderAttribute”。如果我定义类似“可订购”,我需要检查DEfaultOrder是否也设置 – Tekno