在仅知道其名称的对象上更新属性 [英] Update a property on an object knowing only its name

查看:25
本文介绍了在仅知道其名称的对象上更新属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些定义如下的公共变量:

I have some public variables that are defined as follows:

public class FieldsToMonitor
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int Rev {get; set;}
}

我现在想用值填充这些变量,但是 fm.[变量名] 必须与 field.Name 相同.如果我提前知道属性名称和属性名称的顺序,这里将如何循环填充:

I now want to populate those variable with values, but the fm.[varible name] needs be same as field.Name. Here how I would populate in loop if I knew the property name ahead of time and the order of the property name:

// loop 1
fm.Id = revision.Fields[field.Name].Value;

// ... loop 2 ...
fm.Title = revision.Fields[field.Name].Value;

// ... loop 3 ...
fm.Rev = revision.Fields[field.Name].Value;

这是我想做的事情,其中​​ field.Name 可以用属性名代替:

Here is what I would like to do where field.Name can be substituted with property name:

  • fm.ID变为fm.[field.Name]其中field.Name =="ID"

  • fm.ID becomes fm.[field.Name] where field.Name == "ID"

fm.Title变为fm.[field.Name]其中field.Name ==标题"

fm.Title becomes fm.[field.Name] where field.Name == "Title"

fm.Rev成为fm.[field.Name]和where field.Name =="Rev"

fm.Rev becomes fm.[field.Name] and where field.Name == "Rev"

有解决方案吗?

以下是我到目前为止的更多代码:

Here is more code of what I have so far:

public class FieldsToMonitor
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int Rev {get; set;}
}

static BindingList<FieldsToMonitor> FieldsToMonitorList
     = new BindingList<FieldsToMonitor>();
// ...

// Loop through the work item revisions
foreach (Revision revision in wi.Revisions)
{
    fm = new FieldsToMonitor();
    // Get values for the work item fields for each revision
    var row = dataTable.NewRow();
    foreach (Field field in wi.Fields)
    {
        fieldNameType = field.Name;
        switch (fieldNameType)
        {
            case "ID":
            case "Title":
            case "Rev":
                // the following doesn't work
                fm + ".[field.Name]" = revision.Fields[field.Name].Value; 
                fm[field.Name] = revision.Fields[field.Name].Value;
                row[field.Name] = revision.Fields[field.Name].Value;
                break;
        }
    }
}

推荐答案

这全都取决于如何检索这些值,因此很难从有限的示例中分辨出这些值是字符串还是正确的类型装箱作为对象.

This is all heavily dependent on how these values are being retrieved, and it is difficult to tell from your limited example if the values are strings or the correct type just boxed as an object.

话虽如此,以下方法可能有效(但效率不高):

That being said, the following could work (but is hardly efficient):

public static void SetValue<T>(T obj, string propertyName, object value)
{
    // these should be cached if possible
    Type type = typeof(T);
    PropertyInfo pi = type.GetProperty(propertyName);

    pi.SetValue(obj, Convert.ChangeType(value, pi.PropertyType), null);
}

使用方式:

SetValue(fm, field.Name, revision.Fields[field.Name].Value);
// or SetValue<FieldsToMonitor>(fm, ...);

这篇关于在仅知道其名称的对象上更新属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆