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?