I want to call Methods with parameter that contain ref/pointer.
public class Test
{
public void TestMethod(char* pointer, ref reference)
{
}
}
The Problem is I'm not able to generate an Instance of the parameter without getting a Exception "Object does not match target type".
private static Object[] CreateInstanceArray(ParameterInfo[] parameters)
{
if (parameters == null)
return null;
var returnData = new List<Object>();
for (int i = 0; i < parameters.Length; i++)
{
var res = CreateInstance(parameters[i].ParameterType);
returnData.Add(res);
}
return returnData.ToArray();
}
private static Object CreateInstance(Type type)
{
if (type.GetConstructor(Type.EmptyTypes) != null || type.IsValueType) //Suche zuerst den Standardkonstruktor
return Activator.CreateInstance(type);
if (type.IsPointer)//GENERATE POINTER INSTANCE
return CreateInstance(type.GetElementType()).GetType().MakePointerType();
if(type.IsByRef)//GENERATE REF INSTANCE
return CreateInstance(type.GetElementType()).GetType().MakeByRefType();
if(type.IsArray)
return CreateInstance(type.GetElementType()) as Array;
var constructors = type.GetConstructors();
ParameterInfo[] constructorParameter = null;
ConstructorInfo constructor = null;
for(int i = 0; i < constructors.Length; i++)
{
var parameters = constructors[i].GetParameters();
if (constructorParameter == null || constructorParameter.Length > parameters.Length)
{
constructorParameter = parameters;
constructor = constructors[i];
}
}
return constructor.Invoke(type, CreateInstanceArray(constructorParameter));
}
I use the 2 methods like this:
method.Invoke(object, CreateInstanceArray(infos));
Normal methods without Parameter or Arrays/Variables work fine. But I do not know why this Exception is thrown when the parameter contain pointer/ref. Has anyone a idea how I can generate pointer/ref instances during runtime?
Aucun commentaire:
Enregistrer un commentaire