Пример #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
        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);
        }