【发布时间】:2013-10-07 14:44:20
【问题描述】:
我正在使用动态类来生成列表,但是在属性中,我没有得到静态类中的名称,而是 [0]、1、[2] 等。 我需要将这些名称添加到属性中。
有人知道(可能)简单的答案吗?
这是我的代码
更新 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);
更新 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}
从我的角度来看,它们与滑动差异相同,静态我看到关键字段中的关键值和动态 [{key,value}]
任何帮助将不胜感激
【问题讨论】:
-
不是我在乎我在查看器中看到的内容,而是我们使用的 jQuery 对象在乎,因为它无法选择正确的值...
-
您正在使用XY Problem,如果您在构建 JQuery 对象时遇到问题请询问有关构建 JQuery 对象的问题 ,而不是关于如何让它在调试器中正确显示。我建议删除这个问题并提出一个新问题,并确保这次包含更大的图景,假设您正在尝试转换为 JQuery,包括您尝试过的内容,并显示您获得的输出和期望的输出。
-
这正是我的代码要做的,但我不知道这是否适用于 JQuery。您应该询问有关 JQuery 的信息。 (还有我你可以删除不再相关的旧cmets,我删除了我的)
-
构建jQuery对象没有问题,需要有一种动态的方式将数据放入动态列表中,jQuery对象很好用