Esempio n. 1
0
        private DynamicMethodBuilder(string name, MethodType type, Type container, object target, object value, Type owner, bool useBasicTypes)
        {
            this.type             = type;
            this.delegateType     = useBasicTypes ? GetMemberWrapperDelegateType(type) : GetDelegateTypeForInvokeExact(type);
            this.firstBoundValue  = target;
            this.secondBoundValue = value;
            this.container        = container;
            MethodInfo mi = GetDelegateInvokeMethod(delegateType);

            Type[] paramTypes;
            if (container != null)
            {
                this.firstArg = 1;
                paramTypes    = GetParameterTypes(container, mi);
            }
            else if (target != null)
            {
                this.firstArg = 1;
                paramTypes    = GetParameterTypes(target.GetType(), mi);
            }
            else
            {
                paramTypes = GetParameterTypes(mi);
            }
            if (!ReflectUtil.CanOwnDynamicMethod(owner))
            {
                owner = typeof(DynamicMethodBuilder);
            }
            this.dm    = new DynamicMethod(name, mi.ReturnType, paramTypes, owner, true);
            this.ilgen = CodeEmitter.Create(dm);

            if (type.parameterCount() > MaxArity)
            {
                ParameterInfo[] pi = mi.GetParameters();
                this.packedArgType = pi[pi.Length - 1].ParameterType;
                this.packedArgPos  = pi.Length - 1 + firstArg;
            }
            else
            {
                this.packedArgPos = Int32.MaxValue;
            }
        }
    internal static DynamicMethod Create(string name, Type owner, bool nonPublic, Type returnType, Type[] paramTypes)
    {
#if !WINRT
        try
        {
#if NET_4_0
            if (dynamicModule == null)
            {
                // we have to create a module that is security critical to hold the dynamic method, if we want to be able to emit unverifiable code
                AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("<DynamicMethodHolder>"), AssemblyBuilderAccess.RunAndCollect);
                Interlocked.CompareExchange(ref dynamicModule, ab.DefineDynamicModule("<DynamicMethodHolder>"), null);
            }
            return(new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, paramTypes, dynamicModule, true));
#else
            if (!ReflectUtil.CanOwnDynamicMethod(owner))
            {
                // interfaces and arrays aren't allowed as owners of dynamic methods
                return(new DynamicMethod(name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, paramTypes, owner.Module, true));
            }
            else
            {
                return(new DynamicMethod(name, returnType, paramTypes, owner));
            }
#endif
        }
        catch (SecurityException)
        {
            if (nonPublic && !RestrictedMemberAccess)
            {
                // we don't have RestrictedMemberAccess, so we stick the dynamic method in our module and hope for the best
                // (i.e. that we're trying to access something with assembly access in an assembly that lets us)
                return(new DynamicMethod(name, returnType, paramTypes, typeof(DynamicMethodUtils).Module));
            }
            // apparently we don't have full trust, so we try again with .NET 2.0 SP1 method
            // and we only request restrictSkipVisibility if it is required
            return(new DynamicMethod(name, returnType, paramTypes, nonPublic));
        }
#else
        throw new NotImplementedException();
#endif
    }