2013-11-27 19 views
1

enter image description here是他們反正改變下只包括了所有領域的名稱和值有一件事我noticted是測試這個時候它也帶來了entitiy其他信息回我只是想用戶輸入或更改的字段?適應功能,包括字段名稱C#2012.net 3.5

public static string ObjectToNotes(object obj) 
    { 
     if (obj == null) 
     { 
      throw new ArgumentNullException("obj", "Value can not be null or Nothing!"); 
     } 

     StringBuilder sb = new StringBuilder(); 
     Type t = obj.GetType(); 
     PropertyInfo[] pi = t.GetProperties(); 

     for (int index = 0; index < pi.Length; index++) 
     { 
      sb.Append(pi[index].GetValue(obj, null)); 

      if (index < pi.Length - 1) 
      { 
       sb.Append(Environment.NewLine); 
      } 
     } 

     return sb.ToString(); 
    } 

眼下這將節省出的唯一通過

實體的值,你可以從上面的代碼的圖像看到的是越來越值確定和領域,但只是沒有任何下拉列表有關的文字

與此

幫助 需要更多的幫助我如何使用上述方法得到的參考查找值的數值其唯一priting出實體引用而不是實際的文本值可以這樣做

回答

0

假設由你的意思是不是有一個字符串表示爲空或空的請嘗試以下用戶輸入:

var properties = t.GetProperties(); 
var values = properties.Select(p => p.GetValue(obj, null)).Where(v => v != null && !string.IsNullOrEmpty(p.ToString()); 
var result = string.Join(Environment.NewLine, values); 

爲了確定whcih領域發生了變化,你就需要通過兩個對象。一位代表在其後期變化狀態在改變前的狀態的實體和其他和比較的屬性:

var properties = t.GetProperties(); 
var before = properties.Select(p => new { property = p, value = p.GetValue(prechange, null) }).Where(v => v.value != null && !string.IsNullOrEmpty(p.value.ToString()).ToDictionary(p => p.property.Name, p => p.value); 
var after = properties.Select(p => new { property = p, value = p.GetValue(postchange, null) }).Where(v => v.value != null && !string.IsNullOrEmpty(p.value.ToString()).ToDictionary(p => p.property.Name, p => p.value); 
// You can then compare the keys/values of before and after dictionaries here 
+0

okkelly是一些字符串可以爲空,它基本上捕捉到表單上的所有字段,以便它可以將其保存爲審計記錄備份,以便他們不會發生什麼變化 – rogue39nin

+0

您不知道如何使用此方法解析查找值? – rogue39nin

+0

@DavidB已答覆更新 –