I got error which is object must implement IConvertible when converting array to b[] class. The code as follows:
public void Main()
{
ArrayList myList = new ArrayList();
a a1 = new a();
b ba = new b();
ba.b1 = 1;
ba.b2 = 2;
myList.Add(ba);
a1.Bclass = ConvertType(myList.ToArray(), GetArrayType(Type.GetType("b")));
}
private Type GetArrayType(Type elementType)
{
return elementType.MakeArrayType();
}
public static dynamic ConvertType(dynamic source, Type dest)
{
return System.Convert.ChangeType(source, dest);
}
I would like to do this instead of direct casting since I wanted to do it dynamically. The example classes are as follows:
public class a
{
private b[] bclass;
public b[] Bclass
{
get;
set;
}
}
public class b
{
public int b1
{
get;
set;
}
public int b2
{
get;
set;
}
}