-1

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.

---updated---- I put the sample code here

struct Person
{
    public Address[] addresses;
}

struct Address
{
    public int nCode;
    public string homeAddress;
}
class Program
{
    static void Main(string[] args)
    {
        Person p = new Person();
        int nSize = 100;//hard code instead of read from file
        var fields = typeof(Person).GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        foreach (var fieldInfo in fields)
        {
            if (fieldInfo.FieldType.IsArray)
            {
                Array arr = Array.CreateInstance(fieldInfo.FieldType.GetElementType(), nSize);

                //1. Using reflection to set value
                fieldInfo.SetValue(p, arr);

                if(p.addresses == null)
                {
                    Console.WriteLine("Reflection-Not success");
                }
                else
                {
                    Console.WriteLine("Reflection-success");
                }

                //2. Using assignment operator with explicit cast

                p.addresses = (Address[])arr;

                if (p.addresses == null)
                {
                    Console.WriteLine("cast-Not success");
                }
                else
                {
                    Console.WriteLine("cast-Success!!");
                }
            }
        }
    }
}
  • Please add the code how do you get `arrayFieldInfo` and type for which you method fails. Currently can't [reproduce](https://dotnetfiddle.net/U32JXJ). – Guru Stron Jul 23 '21 at 17:32
  • The code you posted does not throw the exception you claim it does. However, it _does_ have the classic "setting field of struct via reflection" bug, where you are operating on a boxed _copy_ of your original value and so the field's value in the original value isn't changed. See duplicate. – Peter Duniho Jul 24 '21 at 05:02

1 Answers1

0

It looks to me like 'arrayFieldInfo' is representing a field whose type isn't Array and isn't implicitly convertible from Array. I'd recommend verifying what field the variable is getting assigned. This could be done with something like the below code, inserted before the SetValue() call. Then, the info should appear in the console output at runtime.

System.Diagnostics.Debug.WriteLine(arrayFieldInfo.ToString());