Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c# - Get property value in List<> instead of [0],[1]

I am using a dynamic class to generate a list, however in the property ;ist I dont get the names as you get in a static class but [0],1,[2], ect. I need these names into the properties.

Anybody knows the (probably) simple answer?

here my code

UPDATE 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);
}

My dynamic class is:

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;
        }
    }
}

Which should give me the same result as:

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; }
}

If I look to the property on runtime from the DynamicClassList I see with the dynamic (sorry dont know how to upload image) something like this:

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

While in the static Class it does correct as I need

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

enter image description here

From my point of view they are the same with the slide difference that with static I see the key values in the key field and with the dynamic in a [{key,value}]

Any help will be appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use a DebuggerTypeProxy and a few specialized classes.

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: To remote the quotes around the key name add nq to the Name property.

[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;
    }
}

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...