繁体   English   中英

为C#中的class属性动态赋值

[英]Dynamically assign values to the class properties in C#

我有一个 class,如下所示。

public class MyClass
{
    public DateTime Date
    { get; set;}

    public double Col1
    { get; set;}

    public double Col2
    { get; set;}

    public double Col3
    { get; set;}

我有一个值列表

Dictionary<string, double> Items 
Items["AA", 23.56] // these AA, BB values will change. 
Items["BB", 15.456] etc

我想设置 MyClass 的属性值。 这是否可能如下所示:

MyClass obj = new MyClass();
int i = 0;

foreach (KeyValuePair<string, double> entry in items)
{
    // take the first property (Col1) and assign the value.
    obj[i] = entry.value;   // is this possible
    i++;
}

或者

在这里,我使用反射遍历 class 属性,并从 Dictionary 中找到每个值,然后将其分配给 Myclass 属性。

MyClass obj = new MyClass();
PropertyInfo[] properties = typeof(MyClass).GetProperties();
foreach (PropertyInfo property in properties)
{
    var value = // take the value from Dictionary.
    property.SetValue(obj, value);
}

问题的目的是在运行时设置 MyClass 的属性值。 我不知道有多少项目会提前出现在字典中。 最后它会像,

       MyClass obj = new MyClass();
       obj.Col1 = 23.56 // value of AA
       obj.Col2 = 15.456 // value of BB
       obj.Col3 = 100.23 // value of CC...it goes like this.

这可能吗? 如果是,如何?

最后我自己做了。

        while (csv.Read())
        {
            string d0 = csv.GetField<string>("DATETIME");
            double? d5 = csv.GetField<double?>("SECONDS");

            if (!string.IsNullOrEmpty(d0))
            {
                DateTime dt = DateTime.Parse(d0, CultureInfo.InvariantCulture).AddMilliseconds(d5.HasValue == true ? d5.Value : 0);                                   

                ModelData md = new ModelData();
                md.Date = dt;

                PropertyInfo[] properties = typeof(ModelData).GetProperties();                    

                foreach (string str in columns)
                {
                    var d1 = csv.GetField<double>(str);
                    foreach (PropertyInfo property in properties)
                    {
                        if(property.PropertyType == typeof(Double?))
                        {
                            if (property.GetValue(md) == null)
                            {
                                property.SetValue(md, d1);
                                break;
                            }
                        }                            
                    }
                }               
                lstValues.Add(md);
               
            }              

您可以尝试如下所示的 ExpandoObject。 它可能会解决你的问题。

dynamic expando = new ExpandoObject();
var expandoDict = expando as IDictionary<string, object>;

foreach (KeyValuePair<string, double> entry in items)
{

    if (expandoDict.ContainsKey(entry.Key))
        expandoDict[entry.Key] = entry.Value;
    else
        expandoDict.Add(entry.Key, entry.Value);
}

更多详情请点击这里

它就像一个 Class(见下文)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM