如果屬性是用戶定義的,我現在可以在使用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);
}
用戶定義?你的意思是非繼承或未覆蓋? – 2011-01-27 07:26:14
我會盡力澄清我需要的東西 – 2011-01-27 15:30:21
你的更新還不是很清楚。然而,一些猜測:您正在尋找一種合理的方式來判斷*屬性是否屬於某些特定類型集合中的某個類型*,以便在`GetFullNameScalarProperties`中決定是否向下鑽取到特定屬性的類型以及檢查其屬性。對? – 2011-02-02 20:10:29