如何从方法内部动态更新类的不同字段?

时间:2011-12-29 09:52:05

标签: c#

  

可能重复:
  How can I code a C# function to accept a variable number of parameters?

我有以下课程:

public class Product : AuditableTable  
{
    public string Position { get; set; }
    public string Quantity { get; set; }
    public double Location { get; set; }
}

我需要的是能够使用以下函数更新类中的字段。

参数:

  • ac和pr定义键并使我能够获得该类的实例。
  • fld是要更新的类的字段名称。它可能是"位置","数量"或"位置"或?? ??
  • val是值。它可能像"伦敦"或" 1.234"

如何在不使用case语句的情况下安排动态设置字段名称 检查fld的每个值和许多不同的setter。此外,如果有某种方式设置字段 动态地如何处理将其转换为该字段的正确对象类型?

    public void Update(string ac, string pr, string fld, string val) 
    { 
        try 
        { 
            vm.Product = _product.Get(ac, pr);
            vm.Product. xxx = fld 
        }
        catch (Exception e) { log(e); }
    }

更新

以下是Pieter提出的解决方案:

public void Update(string ac, string pr, string fld, string val) { 
            try { 
                vm.Product = _product.Get("0000" + ac, pr);
                if (vm.Product != null)
                {
                    var property = vm.Product.GetType().GetProperty(fld);
                    var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                    val = Convert.ChangeType(val, type);
                    property.SetValue(vm.Product, val, null); 
                } 
                _product.AddOrUpdate(vm.Product);
            }
            catch (Exception e) { log(e); }
        }

3 个答案:

答案 0 :(得分:6)

您可以使用反射:

vm.Product.GetType().GetProperty(field).SetValue(vm.Product, val, null);

SetValue要求值的类型正确。如果不是这种情况,您可以使用以下内容进行转换:

var property = vm.Product.GetType().GetProperty(field);

// Convert.ChangeType does not work with nullable types, so we need
// to get the underlying type.

var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;

object convertedValue = Convert.ChangeType(val, type);

property.SetValue(vm.Product, convertedValue, null);

在分配之前,会自动将val转换为属性类型。

但请注意,使用反射和Convert.ChangeType都非常慢。如果你这么做,你应该看一下DynamicMethod

答案 1 :(得分:2)

反思是你可以使用的。

Type myType = Vm.Product.GetType();
PropertyInfo pinfo = myType.GetProperty(fld);
//get property type
Type convertType = pinfo.PropertyType;
//convert it to the required type before setting the property value
var newValue = Convert.ChangeType(val, convertType);

// Use the SetValue method to change the caption.
pinfo.SetValue(vm.Product, newValue , null);

唯一需要记住的是,如果属性是double,那么传递的字符串应该可以转换为double,否则就会产生运行时异常

此处有更多信息:http://msdn.microsoft.com/en-us/library/axt1ctd9.aspx

答案 2 :(得分:1)

在c#中使用反射

    public void Update(string ac, string pr, string fld, string val) { 
    try 
       { 
           vm.Product = _product.Get(ac, pr);
           if( vm.Product != null)
             {
                Type type =  vm.Product.GetType();
                PropertyInfo pIn = type.GetProperty(fld);
                if(pIn != null)
                     pIn.SetValue(vm.Product, val, null);

             }
        }
        catch (Exception e) { log(e); }
     }