vendredi 23 juillet 2021

C# error CS0266: Cannot implicitly convert type 'System.Array'

I want to create a function to dynamic allocate array member element. The array's sizes are different and they get from a file. I use reflection to get array member element from struct object and assign it with the new array created from Array.CreateInstance() function.

private static void initArray<T>(ref T item, BinaryReader br){
var fields = item.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
//....
// get array field from object
//...
 arrayType = arrayFieldInfo.FiledType;
 int length = br.ReadInt32();
 Array arr = Activator.CreateInstance(arrayType, length) as Array;
 arrayFieldInfo.SetValue(item, arr); // this code not work
}

I found that the function not work because of this error: error CS0266: Cannot implicitly convert type 'System.Array'. Please help me.





Aucun commentaire:

Enregistrer un commentaire