mardi 23 janvier 2018

C# PropertyInfo SetValue and Array

int size = Convert.ToInt32(sizeAttribute.Value);
Array newArray =  Array.CreateInstance(fieldType.GetElementType(), size);
PropertyInfo info = component.GetType().GetProperty(fieldNameAttribute.Value, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
var r = Convert.ChangeType(newArray, fieldType);
info.SetValue(component, r);

is there a way to let the array be filled with nulls.

Right now the line info.SetValue(component, r); is returning this error :

{System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.





Aucun commentaire:

Enregistrer un commentaire