我正在使用動態類來生成列表,但是在屬性中; 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}
從我的角度來看,他們與滑差與靜態我看到一個在關鍵領域,並與動態的鍵值[{鍵,值}]
任何幫助將不勝感激
無不是我關心我的觀衆看到,但我們使用jQuery對象關心的,因爲它不能選擇合適的值... –
您正在下降到[XY問題](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem),如果您在構建JQuery對象時遇到問題***,那麼可以詢問構建JQuery對象** *,而不是如何讓它在調試器中正確顯示。我建議刪除這個問題,並提出一個新問題,並確保這次包含更大的圖片,假設您正在嘗試轉換爲JQuery,包含您嘗試的內容,並顯示您獲得的輸出以及您期望的輸出。 –
http://imgur.com/OIiYQ0f –