2017-03-13 8 views
0

Ich hatte ein übergeordnetes Objekt und möchte das untergeordnete Objekt dynamisch durchlaufen.Dynamisch zum untergeordneten Objekt navigieren

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; } 

} 

wenn ich MyProperty in class3 lesen müssen brauche ich ungefähr wie

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

, die ich dynamisch Reflexionen mit tun wollen tun

Bitte Hilfe

+1

Sie können wahrscheinlich erreichen Sie dies durch Reflexion, aber die Frage ist warum? Was ist das Problem, versuchen Sie zu lösen? – Pikoh

+2

Warum Reflexion? Warum brauchst du überhaupt etwas anderes? Dein aktueller Code sollte schon funktionieren. –

+1

Bitte zeigen Sie, was Sie bisher versucht haben. –

Antwort

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; 

    } 
Verwandte Themen