2017-03-13 7 views
0

私は親オブジェクトを持っていて、子オブジェクトに動的に移動する必要があります。私はClass3にでMYPROPERTYを読むために必要がある場合子オブジェクトへの動的な移動

public class Class1 
{ 
    public Class1() 
    { 
     this.MyProperty1 = new List<Class2>(); 
    } 

    public int MyProperty { get; set; } 
    public List<Class2> MyProperty1 { get; set; } 
} 
class Class2 
{ 
    public Class2() 
    { 
     this.MyProperty2 = new List<Class3>(); 
    } 

    public int MyProperty { get; set; } 
    public List<Class3> MyProperty2 { get; set; } 
} 
class Class3 
{ 
    public int MyProperty { get; set; } 

} 

iは

が助けてください、私は反射

を使用して動的に行いたい

Class1.MyProperty1.Last().MyProperty2.Last().MyProperty 

のように気にいらを行う必要があります

+1

することができますおそらく反射を使用してこれを達成するが、質問はなぜですか?あなたは解決しようとしている問題は何ですか? – Pikoh

+2

なぜ反射ですか?なぜ何か他のものが必要なのですか?あなたの現在のコードはすでに動作するはずです。 –

+1

あなたがこれまでに試したことを示してください。 –

答えて

0
public List<string> ChildPath(string ChildObject, List<string> ParentNameList, string AssembleyPath, string NameSpace, List<string> TraversedNodes) 
    { 
     Type ChildType = Type.GetType(NameSpace + ChildObject + AssembleyPath); 
     List<string> Properties = ChildType.GetProperties().Where(x => x.PropertyType.IsClass && x.PropertyType.Assembly.GetName().Name != "mscorlib").Select(x => x.Name).ToList(); 

     ParentNameList.Add(ConfigurationManager.AppSettings[ChildObject].ToString());   
     // List<string> Properties = ConfigurationManager.AppSettings["Child_" + ChildObject].ToString().Split(',').ToList(); 
     int MaxIndex = 0; 
     ChildObject = null; 
     foreach (string item in Properties) 
     { 
      int PresIndex = TraversedNodes.IndexOf(item); 
      if (MaxIndex <= PresIndex) 
      { 
       MaxIndex = PresIndex; 
       ChildObject = item; 
      } 
     } 
     if (ChildObject != null) 
      PathTravarse(ChildObject, ParentNameList, AssembleyPath, NameSpace, TraversedNodes); 
     return ParentNameList; 
    } 


public object GetLastChild(object DataObject, List<string> Path) 
    { 

     object Destination = DataObject; 
     int Count = Path.Count; 
     for (int i = 0; i < Count; i++) 
     { 
      PropertyInfo ChildProperty = Destination.GetType().GetProperty(Path[i]); 
      Destination = ChildProperty.GetValue(Destination, null); 
      Destination = ((IList)Destination)[((IList)Destination).Count - 1];    
     } 

     return Destination; 

    } 

     public object CreateChildInstance(object DataObject, List<string> Path, string ChildName,string AssembleyPath,string NameSpace) 
    { 

     object Destination = DataObject; 
     int Count = Path.Count - 1; 
     for (int i = 0; i < Count; i++) 
     { 
      PropertyInfo ChildProperty = Destination.GetType().GetProperty(Path[i]); 
      Destination = ((IList)ChildProperty.GetValue(Destination, null)); 
      Destination = ((IList)Destination)[((IList)Destination).Count - 1]; 
     } 
     PropertyInfo FinalChild = Destination.GetType().GetProperty(Path[Count]); 
     Destination = FinalChild.GetValue(Destination, null); 
     Type InstanceType = Type.GetType(NameSpace + ChildName + AssembleyPath); 
     Destination.GetType().GetMethod("Add").Invoke(Destination, new[] { Activator.CreateInstance(InstanceType) }); 
     return DataObject; 

    } 
関連する問題