2013-10-07 57 views
-3

我正在使用動態類來生成列表,但是在屬性中; ist我沒有得到名稱,因爲您在靜態類中獲取但[ 0],1,[2]等。 我需要這些名稱到屬性中。在列表<>中獲取屬性值而不是[0],[1]

任何人都知道(可能)簡單的答案?

這裏我的代碼

更新1:

List<string> Properties = new List<string>(); 
     Properties.Add("name"); 
     Properties.Add("instituteTypeId"); 
     Properties.Add("city"); 
     Properties.Add("id"); 

List<DynamicClass> DynamicClassList = new List<DynamicClass>(); 

      int i = 0; 
      foreach (DataRow r in _data.Rows) 
      { 
       DynamicClass dynamicClass = new DynamicClass(); 
      foreach (String Property in Properties) 
       { 
       dynamicClass.property[Property.ToString()] = _data.Rows[i][Property].ToString(); // private string RandomString(int size) 
      } 
      DynamicClassList.Add(dynamicClass); 
} 

我的動態類是:

public class DynamicClass 
{ 
    // property is a class that will create dynamic properties at runtime 
    private DynamicProperty _property = new DynamicProperty(); 

    public DynamicProperty property 
    { 
     get { return _property; } 
     set { _property = value; } 
    } 
} 
public class DynamicProperty 
{ 
    // a Dictionary that hold all the dynamic property values 
    private Dictionary<string, object> properties = new Dictionary<string, object>(); 

    // the property call to get any dynamic property in our Dictionary, or "" if none found. 
    public object this[string name] 
    { 
     get 
     { 
      if (properties.ContainsKey(name)) 
      { 
       return properties[name]; 
      } 
      return ""; 
     } 
     set 
     { 
      properties[name] = value; 
     } 
    } 
} 

應該給我相同的結果:

Medical data = new Medical(); 
data.Name = _data.Rows[i]["name"].ToString(); 
data.instituteTypeId = Convert.ToInt32(_data.Rows[i]["instituteTypeId"].ToString()); 
data.City = _data.Rows[i]["city"].ToString(); 
data.ID = Convert.ToInt32(_data.Rows[i]["id"].ToString()); 
list.Add(data); 

UPDATE 2:

public class Medical 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
    public string City { get; set; } 
    public int instituteTypeId { get; set; } 
} 

如果我看的財產上運行時從DynamicClassList我與動態看(抱歉不知道如何上傳圖片)是這樣的:

key value 
[0] [{id,1}] 
[1] [{name, Med 1}] 

而在靜態類它正確的,因爲我需要

key value 
[id] {1} 
[name] {Med 1} 

enter image description here

從我的角度來看,他們與滑差與靜態我看到一個在關鍵領域,並與動態的鍵值[{鍵,值}]

任何幫助將不勝感激

+0

無不是我關心我的觀衆看到,但我們使用jQuery對象關心的,因爲它不能選擇合適的值... –

+2

您正在下降到[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem),如果您在構建JQuery對象時遇到問題***,那麼可以詢問構建JQuery對象** *,而不是如何讓它在調試器中正確顯示。我建議刪除這個問題,並提出一個新問題,並確保這次包含更大的圖片,假設您正在嘗試轉換爲JQuery,包含您嘗試的內容,並顯示您獲得的輸出以及您期望的輸出。 –

+0

http://imgur.com/OIiYQ0f –

回答

1

同樣你需要使用DebuggerTypeProxy和一些專門的類。

public class Program 
{ 
    private static void Main(string[] args) 
    { 
     DynamicClass dynamicClass = new DynamicClass(); 
     dynamicClass.Property["Test"] = 1; 
     dynamicClass.Property["test2"] = "foo"; 

     Debugger.Break(); 
    } 
} 


[DebuggerTypeProxy(typeof(DynamicClassProxy))] 
public class DynamicClass 
{ 
    // property is a class that will create dynamic properties at runtime 
    private DynamicProperty _property = new DynamicProperty(); 

    public DynamicProperty Property 
    { 
     get { return _property; } 
     set { _property = value; } 
    } 

    [DebuggerDisplay("{value}", Name = "{key}")] 
    private class KeyValuePair 
    { 
     private string key; 
     private object value; 

     public KeyValuePair(KeyValuePair<string,object> kvp) 
     { 
      this.value = kvp.Value; 
      this.key = kvp.Key; 
     } 
    } 

    private class DynamicClassProxy 
    { 
     private DynamicClass _dynamicClass; 
     public DynamicClassProxy(DynamicClass dynamicClass) 
     { 
      _dynamicClass = dynamicClass; 
     } 

     [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] 
     public KeyValuePair[] Keys 
     { 
      get 
      { 
       return _dynamicClass.Property.properties.Select(x => new KeyValuePair(x)).ToArray(); 
      } 
     } 
    } 

    public class DynamicProperty 
    { 
     //Make it so no one can create these objects. 
     internal DynamicProperty() { } 

     // a Dictionary that hold all the dynamic property values 
     internal Dictionary<string, object> properties = new Dictionary<string, object>(); 

     // the property call to get any dynamic property in our Dictionary, or "" if none found. 
     public object this[string name] 
     { 
      get 
      { 
       if (properties.ContainsKey(name)) 
       { 
        return properties[name]; 
       } 
       return ""; 
      } 
      set 
      { 
       properties[name] = value; 
      } 
     } 
    } 
} 

UPDATE:要遠程圍繞重點名字引號添加nqName財產。

​​

enter image description here

+0

仍然無法正常工作,不知何故,我需要鑰匙作爲鑰匙,並且不接受一個字符串,用靜態類測試它,並且可以正常工作(與以前一樣) –

+0

就像我說的,讓它在調試器中顯示並讓它在jQuery中工作是非常不同的。沒有這種類型叫'鑰匙',所以我不知道如何製作'鑰匙'類型的鑰匙。我第三次推薦,專門提出一個新問題,讓您的動態類與jQuery一起工作。 –

+0

好吧,我會這樣做,謝謝你)))) –

相關問題