2011-01-27 41 views
1

如果屬性是用戶定義的,我現在可以在使用Type.GetProperties()獲得的列表中獲取?屬性是用戶定義的?

例如

class test 
{ 
    public string propertyOne{get;set;} 
    public string propertyTwo{get;set;} 
} 

隨着typeof(test).GetProperties()我得到兩個的PropertyInfo,我怎麼能現在他們是用戶定義的?

有關上下文的信息,這裏是一個應該通過

[Test] 
    public void GetFullNameScalarPropertiesTest() 
    { 
     // Act 
     var properties = ReflectionHelper.GetFullNameScalarProperties(typeof(Parent)); 

     // Assert 
     Assert.True(properties.Contains("PropertyOne")); 
     Assert.True(properties.Contains("Child.PropertyTwo")); 
     Assert.True(properties.Contains("Child.GrandChild.PropertyThree")); 
     Assert.That(properties.Count, Is.EqualTo(3)); 
    } 

    class Parent 
    { 
     public Parent() 
     { 
      Child = new Child(); 
     } 

     public string PropertyOne { get; set; } 
     public Child Child { get; set; } 
    } 

    class Child 
    { 
     public Child() 
     { 
      GrandChild = new GrandChild(); 
     } 

     public string PropertyTwo { get; set; } 
     public GrandChild GrandChild { get; set; } 
    } 

    class GrandChild 
    { 
     public string PropertyThree { get; set; } 
    } 

因此,在遞歸方法我得到的屬性和創建與名稱

ATM是通過代碼清單測試此測試是

public static IList<string> GetFullNameScalarProperties(Type type) 
    { 
     var lista = new List<string>(); 
     var path = string.Empty; 
     var properties = type.GetProperties(); 

     foreach (var propertyInfo in properties) 
      GetFullNameScalarProperties(propertyInfo, path, lista); 

     return lista; 
    } 

    private static void GetFullNameScalarProperties(PropertyInfo propertyInfo, string path, ICollection<string> lista) 
    { 
     if (!string.IsNullOrEmpty(path)) 
      path += "."; 

     path += propertyInfo.Name; 

     if (propertyInfo.PropertyType.FullName != null) 
      if (propertyInfo.PropertyType.FullName.StartsWith("System")) 
      { 
       lista.Add(path); 
       return; 
      } 

     var properties = propertyInfo.PropertyType.GetProperties(); 

     foreach (var pi in properties) 
      GetFullNameScalarProperties(pi, path, lista); 
    } 
+2

用戶定義?你的意思是非繼承或未覆蓋? – 2011-01-27 07:26:14

+0

我會盡力澄清我需要的東西 – 2011-01-27 15:30:21

+0

你的更新還不是很清楚。然而,一些猜測:您正在尋找一種合理的方式來判斷*屬性是否屬於某些特定類型集合中的某個類型*,以便在`GetFullNameScalarProperties`中決定是否向下鑽取到特定屬性的類型以及檢查其屬性。對? – 2011-02-02 20:10:29

回答

0

沒有什麼像'用戶定義'屬性。要了解繼承層次結構中聲明瞭屬性的哪種類型,請參閱PropertyInfo.DeclaringType

2

目前還不清楚「用戶定義」是什麼意思 - 您是否試圖發現自動執行的屬性與手動編寫的屬性之間的區別?如果是這樣,一個自動實現的屬性將在getter和setter上具有[CompilerGenerated]屬性。

using System; 
using System.Runtime.CompilerServices; 

class Program 
{ 
    public int AutomaticallyImplemented { get; set; } 
    public int HandWritten { 
     get { return 0; } 
     set {} 
    } 

    static void Main() 
    { 
     foreach (var property in typeof(Program).GetProperties()) 
     { 
      bool auto = property.GetGetMethod().IsDefined 
       (typeof(CompilerGeneratedAttribute), false); 
      Console.WriteLine("{0}: {1}", property.Name, auto); 
     } 
    } 
} 

顯然,你通常要檢查是否有一個getter第一:)

0

如果你想Findout是財產是否被繼承比較PropertyInfo.DeclaringType的類型你正在測試

0

也許你想知道哪一個不是.NET Framework的。

您可以調用Type.GetProperties並使用LINQ迭代找到的屬性,以瞭解這些在您的類中定義的位置以及在框架級別定義的位置。

正如其他人所說,您需要PropertyInfo.DeclaringType來知道某些屬性是在哪裏定義的。如果你想獲得那些非繼承的成員,試圖Type.GetProperties

someObject.GetType().GetProperties().Where(propInfo => propInfo.DeclaringType.IsSubclassOf(typeof(ProjectBaseType)) 
0

,並通過BindingFlags.DeclaredOnly作爲參數:

如果任何項目的對象是從一些基類繼承,也許你可以做到這一點如:

var properties = typeof(test).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);