예제 #1
0
        public Delegate CreateInvoker(Type delegateType)
        {
            MethodSignature sig    = MethodSignature.FromDelegateType(delegateType);
            var             pTypes = new Type[sig.ParameterTypes.Length + 1];

            pTypes[0] = TypeOf <VarArgsDelegate> .TypeID;
            sig.ParameterTypes.CopyTo(pTypes, 1);
            DynamicMethod dyn = new DynamicMethod("", sig.ReturnType, pTypes, TypeOf <VarAgsDelegateBase <TDelegateBase> > .TypeID);
            var           il  = dyn.GetILGenerator();

            for (int i = 0; i < pTypes.Length; i++)
            {
                il.EmitLdarg(i);
            }
            il.EmitCall(OpCodes.Callvirt, InvokeMethod, sig.ParameterTypes);
            if (sig.ReturnType != typeof(void))
            {
                if (sig.ReturnType.IsValueType)
                {
                    il.Emit(OpCodes.Unbox_Any, sig.ReturnType);
                }
                else if (sig.ReturnType != TypeOf <object> .TypeID)
                {
                    il.Emit(OpCodes.Castclass, sig.ReturnType);
                }
            }
            else
            {
                il.Emit(OpCodes.Pop);
            }
            il.Emit(OpCodes.Ret);
            return(dyn.CreateDelegate(delegateType, this));
        }
예제 #2
0
 public static MethodSignature GetDelegateSignature(this Type tDelegate)
 {
     return(MethodSignature.FromDelegateType(tDelegate));
 }