【问题标题】:Get property value in List<> instead of [0],[1]在 List<> 中获取属性值,而不是 [0],[1]
【发布时间】: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对象很好用

标签: c# list dynamic


【解决方案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;
            }
        }
    }
}

更新:要远程引用键名周围的引号,请将 nq 添加到 Name 属性。

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

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

【讨论】:

  • 仍然无法正常工作,不知何故我需要密钥作为密钥并且不接受字符串,使用静态类对其进行了测试,并且可以正常工作(和以前一样)
  • 就像我说的,让它在调试器中显示和让它在 jQuery 中工作是非常不同的。没有这种类型称为Key,所以我不知道如何制作Key 类型的密钥。我第三次推荐,问一个关于让你的动态类与 jQuery 一起工作的新问题。
  • 斯科特,这是我的问题:stackoverflow.com/questions/19283503/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 2013-04-29
  • 2021-06-25
相关资源
最近更新 更多