private object PrepareDelegate(Type parameterType, object val) { Type gty1 = parameterType.GetGenericTypeDefinition(); Type[] gpty1 = parameterType.GetGenericArguments(); MethodInfo method1 = parameterType.GetMethod("Invoke"); if (val is MethodInfo) { MethodInfo method2 = (MethodInfo)val; if (gas.MatchGenericMethod(method1, method2)) { Type[] gpty2 = gas.ConstructGenericArguments(gpty1); parameterType = gty1.MakeGenericType(gpty2); val = Delegate.CreateDelegate(parameterType, null, method2); return(val); } else { return(null); } } else if (val is MulticastDelegate) { MethodInfo method2 = ((MulticastDelegate)val).GetType().GetMethod("Invoke"); if (gas.MatchGenericMethod(method1, method2)) { return(val); } else { return(null); } } else if (val is VAL) { VAL func = (VAL)val; if (func.ty == VALTYPE.funccon) { int argc = DynamicDelegate.FuncArgc(func); Type[] pTypes = DynamicDelegate.GetDelegateParameterTypes(parameterType); if (argc == pTypes.Length) { Type[] gpty2 = gas.ConstructGenericArguments(gpty1); if (gpty2 == null) { throw new HostTypeException("Generic Type is not matched on {0}", parameterType); } parameterType = gty1.MakeGenericType(gpty2); return(DynamicDelegate.ToDelegate(parameterType, val)); } return(null); } } return(null); }
public static Delegate InstanceDelegate(Type dType, VAL func) { DynamicDelegate instance = new DynamicDelegate(func); FieldInfo funcconField = typeof(DynamicDelegate).GetField("func", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo methodAdapter = typeof(DynamicDelegate).GetMethod("CallFunc", BindingFlags.Public | BindingFlags.Static); return(InstanceDelegate(dType, instance, funcconField, methodAdapter)); }
public object CheckCompatibleType(ParameterInfo parameter, object val, Type valType) { Type parameterType = parameter.ParameterType; if (parameterType.IsGenericType) { GenericArgument ga = new GenericArgument(this, parameterType, valType); return(ga.PrepareArgument(val)); } if (parameterType.IsSubclassOf(typeof(MulticastDelegate))) { return(DynamicDelegate.ToDelegate(parameterType, val)); } return(null); }
public static object ToDelegate(Type type, object val) { MethodInfo method1 = type.GetMethod("Invoke"); if (val is MethodInfo) { MethodInfo method2 = (MethodInfo)val; if (CompareMethodSignature(method1, method2)) { val = Delegate.CreateDelegate(type, null, method2); return(val); } } else if (val is MulticastDelegate) { MethodInfo method2 = ((MulticastDelegate)val).GetType().GetMethod("Invoke"); if (CompareMethodSignature(method1, method2)) { return(val); } } else if (val is VAL) { VAL func = (VAL)val; if (func.ty == VALTYPE.funccon) { #if DEBUG_TIE_DELEGATE DynamicDelegate.funccon = func; return(Delegate.CreateDelegate(type, null, typeof(DynamicDelegate).GetMethod("test102"))); #else int argc = DynamicDelegate.FuncArgc(func); Type[] pTypes = DynamicDelegate.GetDelegateParameterTypes(type); if (argc == pTypes.Length) { return(DynamicDelegate.InstanceDelegate(type, func)); } else { return(null); } #endif } } return(null); }
public static int test101(int[] A) { object[] args = new object[1]; args[0] = A; return((int)DynamicDelegate.CallFunc(funccon, args)); }