I am making a tool for defining objects in WPF so they can be serialized into JSON for testing our REST service. It uses reflection to pull all the properties into the GUI, and generates a JSON based on the input. This works fantastically for everything except properties that are arrays of types that I defined. I am using the object.InvokeMember()
method to accomplish this.
The problem is, you have to pass it an object array as the parameter. When its just one thing, its fine, but when its an array, it throws an exception because the object array isn't casted to the type the property is. This is hard to explain.
some code:
private void Button_Click(object sender, RoutedEventArgs e)
{
object obj = new MyType();
List<list<object>> list = new List<List<object>>();
object[] ob = new object[] { list.ToArray() };
obj.GetType().InvokeMember(
"MyOtherTypes",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder,
obj,
ob);
UpdateJson(obj);
}
public class MyType
{
public MyOtherType[] MyOtherTypes { get; set; }
}
public class MyOtherType
{
public int SomeProperty { get; set; }
}
I am sure whoever reads this will be confused, so please ask questions.
More code, an example of modifying a type that is not an array. This part works fine:
object[] ob = new object[] { obj };
obj.GetType().InvokeMember(
"SomePropertyThatIsNotAnArray",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder,
obj,
ob);
UpdateJson(obj);
Been trying something like this, but same deal, cant cast object[] to MyType[]
MethodInfo castMethod = this.GetType().GetMethod("Cast").MakeGenericMethod(
obj.GetType().MakeArrayType());
object castedObject = castMethod.Invoke(
null,
new object[] { list[someIndex].ToArray() });
obj.GetType().InvokeMember(
"SomePropertyThatIsNotAnArray",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
Type.DefaultBinder,
obj,
castedObject);
public static T Cast<T>(object o)
{
return (T)o;
}