Пример #1
0
        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);
        }
Пример #2
0
        private Tuple <MethodInfo, object[]> CheckParameters(MethodInfo method)
        {
            ParameterInfo[] parameters = method.GetParameters();
            int             len        = parameters.Length;

            object[] args2     = new object[len];
            Type[]   argTypes2 = new Type[len];

            if (len > 0 && len < args1.Length)
            {
                bool isParams = Attribute.IsDefined(parameters[len - 1], typeof(ParamArrayAttribute));
                if (isParams)
                {
                    for (int i = 0; i < len - 1; i++)
                    {
                        args2[i]     = args1[i];
                        argTypes2[i] = argTypes1[i];
                    }

                    Type  ty    = parameters[len - 1].ParameterType;
                    Array array = (Array)Activator.CreateInstance(ty, new object[] { args1.Length - len + 1 });
                    for (int i = 0; i < args1.Length - len + 1; i++)
                    {
                        array.SetValue(args1[len + i - 1], i);
                    }

                    args2[len - 1]     = array;
                    argTypes2[len - 1] = ty;
                    goto L1;
                }
            }

            if (len != args1.Length)
            {
                return(null);
            }

            for (int i = 0; i < len; i++)
            {
                args2[i]     = args1[i];
                argTypes2[i] = argTypes1[i];
            }

L1:
            GenericArguments gas = new GenericArguments();

            for (int i = 0; i < len; i++)
            {
                if (!HostOperation.IsCompatibleType(parameters[i].ParameterType, args2[i], argTypes2[i]))
                {
                    if (args2[i] == null && argTypes2[i] == null)
                    {
                        return(null);
                    }

                    object temp = gas.CheckCompatibleType(parameters[i], args2[i], argTypes2[i]);
                    if (temp == null)
                    {
                        return(null);
                    }

                    args2[i] = temp;
                }
            }

            if (method.IsGenericMethod && method.IsGenericMethodDefinition)
            {
                Type[] gaty2 = gas.ConstructGenericArguments(method.GetGenericArguments());
                method = method.MakeGenericMethod(gaty2);
            }

            return(Tuple.Create(method, args2));
        }