2011-12-29 14 views
3

可能重複:
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; } 
} 

我需要的是能夠與下面的函數來更新類領域。

參數:

  • 交流和Pr定義的按鍵,使我得到這個類的一個實例。
  • fld是要更新的類的字段名稱。它可以是「位置」,「數量」或「位置」或?
  • val是值。這可能是類似「倫敦」或「1.234」

我如何安排進行動態設置的字段名稱不使用的情況下,語句 檢查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); } 
    } 

更新

這裏是由彼得提出的解決方案:

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); } 
     } 
+1

嗯,還沒有這得到回答[這裏](HTTP://計算器。COM/A /1106367分之8663926)? – Adam 2011-12-29 10:04:21

+0

重複創建有什麼意義? – codeSetter 2011-12-29 10:09:57

+0

以前給出的答案不起作用,因爲設置值試圖設置vm而不是vm.Prouct。看看這個線程,我能夠進一步得到一點,但仍然有一個問題,因爲沒有答案將數據類型轉換考慮在內:-( – 2011-12-29 11:13:43

回答

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

+0

感謝Pieter。這是我以前見過的答案,但我遇到了問題它會工作,我會再次檢查,也許我犯了一個錯誤 – 2011-12-29 10:48:38

+0

反射的工作可能會很棘手,如果遇到具體問題,請在這裏發表評論。 – 2011-12-29 11:00:14

+0

感謝您的回答。這裏有一個問題:[System.ArgumentException] = {「類型'System.String'的對象不能轉換爲類型'System.Nullable'1 [System.Double]'。」} val始終是一個字符串,但屬性可能這裏的屬性位置是一個雙重的,所以當我嘗試更新一個字符串時,我得到這個錯誤 – 2011-12-29 11:15:08

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的唯一的事,其他明智的你將有一個運行時異常

更多的信息在這裏:在C#http://msdn.microsoft.com/en-us/library/axt1ctd9.aspx

+0

感謝您的回答。不幸的是,它只適用於數據類型相同的情況。仍然需要一些方法來轉換數據類型。某種形式的演員,但我不知道該怎麼做。 – 2011-12-29 11:16:18

+0

這也可以做到。看我編輯的代碼 – Anand 2011-12-29 11:29:15

1

使用反射

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); } 
    } 
+0

你好。看到我上面的評論。仍然是一個問題,因爲它只適用於val和屬性的數據類型匹配。 – 2011-12-29 11:17:02

相關問題