как я могу получить свойства подкласса и установить значение? - PullRequest
0 голосов
/ 05 августа 2020

Я пытаюсь получить все свойства из подкласса, а затем установить значение. но я не знаю, с чего начать. Я знаю, как получить свойства и установить значение. но не для подкласса.

public class Program {
  public static void Main(string[] args) {

    object[] Response = new object[] {
      new Cars() {
        Details = new Details() {
          CanDrive = true,
          ID = 123,
          IsPolice = true
        },
        Name = "test",
        Speed = 100
      }
    };

    var Result = Test <Cars> (Response);
    Console.WriteLine(Result.Speed);

  }
  private static T Test <T> (object[] Obj) {
    var Instance = CreateInstance <T> ();

    foreach(var Ob in Obj) {

      PropertyInfo[] C = Ob.GetType().GetProperties();
      foreach(var n in C) {

        PropertyInfo[] P = typeof(T).GetProperties();

        foreach(PropertyInfo Val in P) {
          if (n.Name == Val.Name) V
              Val.SetValue(Instance, n.GetValue(Ob));
        }
      }
    }

    return Instance;
  }
  private static T CreateInstance <T>() => Activator.CreateInstance<T>();
  public class Cars {
    public int Speed { get; set; }
    public string Name { get; set; }
    public Details Details { get; set; }
  }
  public class Details {
    public int ID { get; set; }
    public bool CanDrive { get; set;}
    public bool IsPolice { get; set; }
  }
}

как я могу получить подклассы? и установить значение класса?

Edit Обновил мой код. для лучшего понимания.

Ответы [ 2 ]

0 голосов
/ 05 августа 2020
public class Program
{
    public static void Main(string[] args)
    {
        
        var CarInstance = CreateInstance<Cars>();

        PropertyInfo[] P = typeof(Cars).GetProperties();

        foreach (PropertyInfo Car in P)
        {
              if(Car.Details.IsPolice) //Access properties of subclass using the name of the property, which is "Details"
                  Car.SetValue(CarInstance, 12345);
            
               if(Car.Name == "ID") 
                  Car.SetValue(CarInstance, 12345);
            
        }
    }
    private static T CreateInstance<T>() => Activator.CreateInstance<T>();
    public class Cars
    {
        public int Speed { get; set; }
        public string Name { get; set; }
        public Details Details { get; set; }
    }
    public class Details
    {
        public int ID { get; set; }
        public bool CanDrive { get; set; }
        public bool IsPolice { get; set; }
    }
}

Это небольшое отличие может помочь устранить некоторую путаницу:

public class Program
{
    public static void Main(string[] args)
    {
        
        var CarInstance = CreateInstance<Cars>();

        PropertyInfo[] P = typeof(Cars).GetProperties();

        foreach (PropertyInfo Car in P)
        {
              if(Car.ThisCarDetails.IsPolice) //Access properties of subclass using the name of the property, which is "ThisCarDetails"
                  Car.SetValue(CarInstance, 12345);
            
               if(Car.Name == "ID") 
                  Car.SetValue(CarInstance, 12345);
            
        }
    }
    private static T CreateInstance<T>() => Activator.CreateInstance<T>();
    public class Cars
    {
        public int Speed { get; set; }
        public string Name { get; set; }
        public Details ThisCarDetails { get; set; }
    }
    public class Details
    {
        public int ID { get; set; }
        public bool CanDrive { get; set; }
        public bool IsPolice { get; set; }
    }
}
0 голосов
/ 05 августа 2020

Я не понимаю, почему мы дошли до рефлексии для этого?

Может ли это быть так же просто, как

 var car = new Car();
        car.Details = new Details()
        {
            CanDrive = true,
            ID = 42,
            IsPolice = false
        };
...