0

All the values are coming at the run time in terms of string.

namespace ConsoleApp2
{
    class myClass1
    {
        public string obj1;
        public string obj2;

    }
    class myClass2
    {
        public myClass1[] fArray = new myClass1[2];
        public Dictionary<string, string> dictObj = new Dictionary<string, string>();

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

            Type type = Type.GetType("ConsoleApp2.myClass2");
            object myObj = Activator.CreateInstance(type);
            FieldInfo fi = type.GetField("fArray");

            IList arr = (IList)fi.GetValue(myObj);

            char[] aray = { '[', ']' };
            Type type2 = Type.GetType((fi.FieldType.FullName.Trim(aray)));

            object newObj;
            List<object> mylist = new List<object>();

            {
                newObj = Activator.CreateInstance(type2);
                FieldInfo fi2 = type2.GetField("obj1");
                fi2.SetValue(newObj, "some txt1");
                FieldInfo fi3 = type2.GetField("obj2");
                fi3.SetValue(newObj, "some txt2");
                arr[0] = newObj;

            }
            {
                newObj = Activator.CreateInstance(type2);
                FieldInfo fi2 = type2.GetField("obj1");
                fi2.SetValue(newObj, "some txt1");
                FieldInfo fi3 = type2.GetField("obj2");
                fi3.SetValue(newObj, "some txt2");
                arr[1] = newObj;

            }

    }
}

In this way, I am able to pass the value correctly to "fArray". is it the correct way or I can make it more straightforward. I would like to know that should I follow the same way for passing the value to the dictionary?

  • _"do we need to initialize the array with values before setting the fArray?"_ -- did you try it without doing so? Is there some _specific difficulty_ you're having? It's not at all clear what you're actually asking here. You may or may not need to initialize the array first, depending on how you want the code to be structured. But as a reference type, it certainly _can_ work if you initialize after assignment. What is it you actually need help with here? – Peter Duniho Mar 10 '20 at 17:00
  • Yes, I want to initialize fArray with some value and then set. – muhayyuddin gillani Mar 10 '20 at 17:28
  • 1
    Have a look at this answer. It solves for the array part and you can use same logic for dictionary: https://stackoverflow.com/a/9783259/13198 – TheVillageIdiot Mar 10 '20 at 17:34
  • it works for fArray. I have updated the code but still, confuse about the dictionary – muhayyuddin gillani Mar 10 '20 at 18:17

2 Answers2

1

If you know it is an array, then it must implement the interfaces of an array including the indexor. Why not simple cast as one of the interfaces. Do you know the types held in the array?

IList<T>
ICollection<T>
IEnumerable<T> - does allow setting

If you cannot discover T then use

IList or ICollection

NeilNewman
  • 95
  • 8
  • Thank you with IList it works for the fArray. I have updated the question with the working code for fArray. now I am curious about the dictionary – muhayyuddin gillani Mar 10 '20 at 18:16
0

It's the other way around, use var array = (myClass1[])fi1.GetValue(); and then just use it as the array it is ;)

Patrick Beynio
  • 788
  • 1
  • 6
  • 13