I am trying to generate some generic code that can create C# objects from text generated by another system. The object is to be used for a method call - the method call is also going to be done by reflection. When I am creating this method parameter object, I could not figure out how to instantiate and assign values to a property that is array type. I can use the setValue to assign to "name" in the code sample below, but how to assign values to the array?
class Car {
public string name { get; set; }
public Door[] doors { get; set; }
}
class Door {
public int index { get; set; }
public bool isDusty { get; set; }
}
public object createMethodParameter(Vehicle<T> v)
object methodParameter;
Type type = v.GetType();
PropertyInfo[] properties;
MethodInfo[] mi = type.GetMethods();
ParameterInfo[] pi;
foreach (var method in mi)
{
if ("create".Equals(method.Name.ToLowerInvariant())) // look for the create method
{
pi = method.GetParameters();
foreach (var param in pi)
{
returnValue = Activator.CreateInstance(param.ParameterType);
properties = param.ParameterType.GetProperties();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType.IsArray)
{
// how to create the doors array on the car??
}
else
{
property.SetValue(methodParameter, "Porsche", null);
}
}
}
}
}
return methodParameter;
}
Aucun commentaire:
Enregistrer un commentaire