1

I have a class that contains several other classes as properties. Initially, none of the properties are instantiated. I would like to instantiate a single property by passing the property itself in the Load function.

public class MyMainClass
{
    public ClassA A { get; set; }
    public ClassB B { get; set; }
    public ClassC C { get; set; }
    // ...
    // ...
    // Many more. All these classes are inherited from Class0

    public void Load(Class0 UnknownClass)
    {
        if ((UnknownClass) is ClassA) A = new ClassA();    
        if ((UnknownClass) is ClassB) B = new ClassB();    
        if ((UnknownClass) is ClassC) C = new ClassC();
        //  and so on... This should to be done in a loop 
    }
}

public void Main()
{
    MyMainClass MyObj = new MyMainClass();
    MyObj.Load(MyObj.ClassA);  // This should instantiate MyObj.ClassA
    MyObj.ClassA.SomeMethod();
}

Class0 is the base class for ClassA, ClassB and so on.

This works fine. But I do not want to write a whole bunch of comparisons for each class. I need to loop through the properties, find a matching type and instantiate it. I probably need to use system.reflection, but not sure how...

There are other similar answers, but each instantiate a new object based on the Type passed. I need to instantiate the property of the class.

navigator
  • 1,678
  • 16
  • 29
  • Possible duplicate of [In C#, how to instantiate a passed generic type inside a method?](https://stackoverflow.com/questions/658951/in-c-how-to-instantiate-a-passed-generic-type-inside-a-method) – pritaeas Aug 21 '18 at 10:28
  • @pritaeas, I don't really think so. I need to instantiate a property of the class. Not a new object. – navigator Aug 21 '18 at 10:30
  • Isn't that still a new object? – pritaeas Aug 21 '18 at 10:31
  • Once instantiated, I need to be able to refer to it directly like`MyObj.ClassA.SomeMethod();` – navigator Aug 21 '18 at 10:33
  • So you are looking for this? https://stackoverflow.com/questions/957783/loop-through-an-objects-properties-in-c-sharp (part of it at least) – pritaeas Aug 21 '18 at 10:36
  • Yes, something similar. I can loop through the properties of my class and match the type with the type of the passed parameter. But once found, how do I instantiate it? – navigator Aug 21 '18 at 10:41
  • With `typeof` you can get the type of the property and use that. Can you update the question to show your new code? – pritaeas Aug 21 '18 at 11:13

1 Answers1

0

Okay, I finally figured it out after combining several other answers:

public void Load<T>() where T : new()
{
    object lObj = Activator.CreateInstance(typeof(T));  // instantiate a new object of type passed

    // Loop through properties and assign object to the property that matches its type
    System.Reflection.PropertyInfo[] lProps = typeof(MyMainClass).GetProperties();
    foreach (System.Reflection.PropertyInfo lProp in lProps)
    {
        if ((typeof(T).ToString() == lProp.PropertyType.Name)) lProp.SetValue(this, lObj);
    }
}
navigator
  • 1,678
  • 16
  • 29