2017-01-30 73 views
-2

我需要獲取班級中所有成員的類型。獲取某個班級所有成員的類型

public class Order 
    { 
     public int OrderID { get; set; } 
     public int EmployeeID { get; set; } 
     public string CustomerID { get; set; } 
     public DateTime OrderDate { get; set; } 
     public bool Verified { get; set; } 

     public Order(int orderId, int empId, string custId, DateTime orderDate, bool verify) 
     { 
      this.OrderID = orderId; 
      this.EmployeeID = empId; 
      this.CustomerID = custId; 
      this.OrderDate = orderDate; 
      this.Verified = verify; 
     } 
    } 

,我已經通過列表

  List<dynamic> dy = new List<dynamic>(); 
      dy.Add(new { OrderID = 11, EmployeeID = 5, CustomerID = "ALFKI", OrderDate = new DateTime(2015,01, 10), Verified = true }); 
      dy.Add(new { OrderID = 12, EmployeeID = 4, CustomerID = "CDSAW", OrderDate = new DateTime(2015, 02, 12), Verified = true }); 
      dy.Add(new { OrderID = 13, EmployeeID = 6, CustomerID = "ASDFG", OrderDate = new DateTime(2015, 03, 13), Verified = true }); 
      dy.Add(new { OrderID = 14, EmployeeID = 3, CustomerID = "XSDAF", OrderDate = new DateTime(2015, 04, 14), Verified = false }); 
      dy.Add(new { OrderID = 15, EmployeeID = 2, CustomerID = "MNHSD", OrderDate = new DateTime(2015, 05, 15), Verified = true }); 
      dy.Add(new { OrderID = 16, EmployeeID = 1, CustomerID = "OKJHG", OrderDate = new DateTime(2015, 06, 16), Verified = false }); 
      return dy; 

添加對於得到的字段類型的類的價值,我想用下面的代碼。

Type type = dataSource.GetElementType(); 
Type t = typeof(object); 
t = type.GetProperty("OrderDate").PropertyType; 

其引發空值表達式錯誤。

此處OrderDate是DateTime對象。

Type type = dataSource.GetType(); 

此行返回System.object。

嘗試獲取OrderDate字段類型時。

type.GetProperty(filterString).PropertyType; 

返回空,如何解決這個問題。

在線:https://dotnetfiddle.net/zTYJGU的一切,如果你想在指定類型的所有字段和屬性類型,你應該知道的一些事情

+0

你像這裏描述的那樣? http://stackoverflow.com/questions/6536163/how-to-list-all-variables-of-class – Essigwurst

+8

這看起來像一個不適當的使用'動態'... –

+0

你可以定義什麼是'dataSource'? –

回答

0

第一。

每個屬性都有自己的後臺字段。對於自動生成的屬性public int MeProperty { get; set; },後臺字段的名稱將爲<MeProperty>k__BackingField

知道這個,你應該考慮提取FieldInfo而不是PropertyInfo

另一件事是,你假設GetProperty方法將提取甚至隱藏(privateprotected etc ..)成員,這是完全錯誤的。它期望您通過使用枚舉來指定訪問級別。

讓所有的知識後,您可以使用只提取出所有屬性的領域instad的:

var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); 

之後,你可以列舉,並得到字段類型:

var fieldTypes = fields.Select(f => f.FieldType); 

現在你fieldTypesIEnumerable<Type>,其中包含type字段中指定的每個字段類型。

真正的問題是你想實現自動映射器,它從動態類型創建你定義的類型。爲此,我建議你使用該解決方案從my answer on this question

編輯:

使用此代碼:

class Program 
{ 
    static void Main() 
    { 
     foreach(var field in typeof(Order).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) 
     { 
      Console.WriteLine(field.Name); 
     } 
    } 

    public class Order 
    { 
     public int OrderID { get; set; } 
     public int EmployeeID { get; set; } 
     public string CustomerID { get; set; } 
     public DateTime OrderDate { get; set; } 
     public bool Verified { get; set; } 

     public Order(int orderId, int empId, string custId, DateTime orderDate, bool verify) 
     { 
      this.OrderID = orderId; 
      this.EmployeeID = empId; 
      this.CustomerID = custId; 
      this.OrderDate = orderDate; 
      this.Verified = verify; 
     } 
    } 
} 

你應該有輸出相同:

<OrderID>k__BackingField                                                        
<EmployeeID>k__BackingField                                                        
<CustomerID>k__BackingField                                                        
<OrderDate>k__BackingField                                                        
<Verified>k__BackingField 
+0

記錄的dy列表Hi Rogalsi,我試着用你的解決方案。但我只得到三個領域,而不是所有的領域 – Kalai