2013-05-07 286 views
0

我需要獲取和設置屬性值動態設置屬性值

我讀這Get property value from string using reflection in C#

,做了下面的代碼用於獲取的值

public Object GetPropValue(Object obj, String name) { 
    foreach (String part in name.Split('.')) { 
     if (obj == null) { return null; } 

     Type type = obj.GetType(); 
     PropertyInfo info = type.GetProperty(part); 
     if (info == null) { return null; } 

     obj = info.GetValue(obj, null); 
    } 
    return obj; 
} 

現在我需要設置值到具有相同屬性名稱的其他對象

Employee emp1=new Employee(); 
var city=GetPropValue(emp1, "Address.City"); 

Need to set this city to other employee. Here Address is other class 

emp1.GetType().GetProperty("Address.City").SetValue(emp2,city,null) //always sets null 

但它是n ot設置。我怎樣才能使一個通用的setter方法使這個工作簡單?

回答

2

這條線是不正確的:

emp2.Address.City= emp1.GetType().GetProperty("Address.City").SetValue(emp2,city,null) 

你試圖設置調用屬性的二傳手給定對象上的emp2.Address.City結果。

爲什麼你想在這種情況下使用反射?鑑於你的代碼行,你可以寫

emp2.​​Address.City = city;

由於您的反射代碼也設置了emp2的屬性。所以即使它起作用了,它也會做同樣的事情兩次。

您的代碼會是這樣書寫:

emp2.Address.City = city; 
emp1.GetType().GetProperty("Address.City").SetValue(emp2,city,null); 
+0

即使我想這樣也。但它總是設置爲空。我們如何設置嵌套的對象屬性? – Billa 2013-05-07 11:44:07

+0

'city'是否有值或是'city'' null'?你有沒有嘗試設置一個斷點? – 2013-05-07 12:14:34

-1
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using System.Text; 
using System.Threading.Tasks; 

namespace LinqTests 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var f1 = new F1 { F2 = new F2 { Name = "Test"}, Q = 10 }; 
      var f3 = new F3 { F2 = new F2() }; 
      Copier.Copy(f1, f3, "Q"); 
      Copier.Copy(f1, f3, "F2.Name"); 

     } 

     static class Copier 
     { 
      public static void Copy(object source, object destination, string navigationPath) 
      { 
       var sourceValuePointHandle = GetValuePointHandle(source, navigationPath); 
       var destinationValuePointHandle = GetValuePointHandle(destination, navigationPath); 
       destinationValuePointHandle.SetValue(sourceValuePointHandle.GetValue()); 
      } 

      private static ValuePointHandle GetValuePointHandle(object instance, string navigationPath) 
      { 
       var propertyName = new String(navigationPath.TakeWhile(x => x != '.').ToArray()); 
       var property = instance.GetType().GetProperty(propertyName); 

       if (propertyName.Length != navigationPath.Length) 
       { 
        var propertyInstance = property.GetValue(instance, null); 
        return GetValuePointHandle(propertyInstance, navigationPath.Substring(propertyName.Length + 1, navigationPath.Length - propertyName.Length - 1)); 
       } 
       else 
        return new ValuePointHandle(instance, property); 
      } 

      class ValuePointHandle 
      { 
       public object Instance 
       { 
        get; 
        private set; 
       } 

       public PropertyInfo Property 
       { 
        get; 
        private set; 
       } 

       public ValuePointHandle(object instance, PropertyInfo property) 
       { 
        Instance = instance; 
        Property = property; 
       } 

       public void SetValue(object value) 
       { 
        Property.SetValue(Instance, value, null); 
       } 

       public object GetValue() 
       { 
        return Property.GetValue(Instance, null); 
       } 
      } 
     } 

     class F1 
     { 
      public int Q 
      { 
       get; 
       set; 
      } 

      public F2 F2 
      { 
       get; 
       set; 
      } 
     } 

     class F2 
     { 
      public string Name 
      { 
       get; 
       set; 
      } 
     } 

     class F3 
     { 
      public int Q 
      { 
       get; 
       set; 
      } 

      public F2 F2 
      { 
       get; 
       set; 
      } 
     } 
    } 

} 
+0

這是一個很大的代碼與零解釋。錯誤的答案。 – 2013-05-07 13:39:52

+0

這只是一種魔力。它不需要解釋。 – user1770543 2013-05-07 13:54:17

+0

我想也許你誤解了這個網站的目的......沒有「魔法」,只有代碼,可以隨時解釋。 – 2013-05-07 14:28:27