2011-10-04 59 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