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 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); }