Example #1
0
        public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (type is TypeBuilder)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
            }
            if ((bindingAttr & 0xff) == BindingFlags.Default)
            {
                bindingAttr |= BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance;
            }
            if ((activationAttributes != null) && (activationAttributes.Length > 0))
            {
                if (!type.IsMarshalByRef)
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
                }
                if (!type.IsContextful && ((activationAttributes.Length > 1) || !(activationAttributes[0] is UrlAttribute)))
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
                }
            }
            RuntimeType underlyingSystemType = type.UnderlyingSystemType as RuntimeType;

            if (underlyingSystemType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
            return(underlyingSystemType.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes));
        }
Example #2
0
        /// <include file='doc\Activator.uex' path='docs/doc[@for="Activator.CreateInstance1"]/*' />
        static public Object CreateInstance(Type type,
                                            BindingFlags bindingAttr,
                                            Binder binder,
                                            Object[] args,
                                            CultureInfo culture,
                                            Object[] activationAttributes)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type is System.Reflection.Emit.TypeBuilder)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
            }

            // If they didn't specify a lookup, then we will provide the default lookup.
            if ((bindingAttr & (BindingFlags)LookupMask) == 0)
            {
                bindingAttr |= Activator.ConstructorDefault;
            }

            try {
                RuntimeType rt = (RuntimeType)type.UnderlyingSystemType;
                return(rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes));
            }
            catch (InvalidCastException) {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
        }
Example #3
0
        [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable
        static public Object CreateInstance(Type type,
                                            BindingFlags bindingAttr,
                                            Binder binder,
                                            Object[] args,
                                            CultureInfo culture,
                                            Object[] activationAttributes)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException("type");
            }
            Contract.EndContractBlock();

            if (type is System.Reflection.Emit.TypeBuilder)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
            }

            // If they didn't specify a lookup, then we will provide the default lookup.
            if ((bindingAttr & (BindingFlags)LookupMask) == 0)
            {
                bindingAttr |= Activator.ConstructorDefault;
            }

            if (activationAttributes != null && activationAttributes.Length > 0)
            {
                // If type does not derive from MBR
                // throw notsupportedexception
#if FEATURE_REMOTING
                if (type.IsMarshalByRef)
                {
                    // The fix below is preventative.
                    //
                    if (!(type.IsContextful))
                    {
                        if (activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute))
                        {
                            throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
                }
#else
                throw new PlatformNotSupportedException(Environment.GetResourceString("NotSupported_ActivAttr"));
#endif
            }

            RuntimeType rt = type.UnderlyingSystemType as RuntimeType;

            if (rt == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }

            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
            return(rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes, ref stackMark));
        }
Example #4
0
        /// <include file='doc\Activator.uex' path='docs/doc[@for="Activator.CreateInstance1"]/*' />
        static public Object CreateInstance(Type type,
                                            BindingFlags bindingAttr,
                                            Binder binder,
                                            Object[] args,
                                            CultureInfo culture,
                                            Object[] activationAttributes)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type is System.Reflection.Emit.TypeBuilder)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
            }

            // If they didn't specify a lookup, then we will provide the default lookup.
            if ((bindingAttr & (BindingFlags)LookupMask) == 0)
            {
                bindingAttr |= Activator.ConstructorDefault;
            }

            if (activationAttributes != null && activationAttributes.Length > 0)
            {
                // If type does not derive from MBR
                // throw notsupportedexception
                if (type.IsMarshalByRef)
                {
                    // The fix below is preventative.
                    //
                    if (!(type.IsContextful))
                    {
                        if (activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute))
                        {
                            throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
                }
            }

            try {
                RuntimeType rt = (RuntimeType)type.UnderlyingSystemType;
                return(rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes));
            }
            catch (InvalidCastException) {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
        }
Example #5
0
        internal static object InternalCreateInstanceWithNoMemberAccessCheck(Type type, bool nonPublic)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            RuntimeType underlyingSystemType = type.UnderlyingSystemType as RuntimeType;

            if (underlyingSystemType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
            return(underlyingSystemType.CreateInstanceImpl(!nonPublic, false, false));
        }
Example #6
0
        public static object CreateInstance(Type type, bool nonPublic)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            RuntimeType underlyingSystemType = type.UnderlyingSystemType as RuntimeType;

            if (underlyingSystemType == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
            return(underlyingSystemType.CreateInstanceImpl(!nonPublic));
        }
Example #7
0
        /// <include file='doc\Activator.uex' path='docs/doc[@for="Activator.CreateInstance8"]/*' />
        static public Object CreateInstance(Type type, bool nonPublic)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            try {
                RuntimeType rt = (RuntimeType)type.UnderlyingSystemType;
                return(rt.CreateInstanceImpl(!nonPublic));
            }
            catch (InvalidCastException) {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
            }
        }
Example #8
0
        [System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
        static public Object CreateInstance(Type type,
                                            BindingFlags bindingAttr,
                                            Binder binder,
                                            Object[] args,
                                            CultureInfo culture,
                                            Object[] activationAttributes)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            Contract.EndContractBlock();

            if (type is System.Reflection.Emit.TypeBuilder)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
            }

            // If they didn't specify a lookup, then we will provide the default lookup.
            if ((bindingAttr & (BindingFlags)LookupMask) == 0)
            {
                bindingAttr |= Activator.ConstructorDefault;
            }

            if (activationAttributes != null && activationAttributes.Length > 0)
            {
                throw new PlatformNotSupportedException(Environment.GetResourceString("NotSupported_ActivAttr"));
            }

            RuntimeType rt = type.UnderlyingSystemType as RuntimeType;

            if (rt == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(type));
            }

            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;

            return(rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes, ref stackMark));
        }
Example #9
0
        static public Object CreateInstance(Type type,
                                            BindingFlags bindingAttr,
                                            Binder binder,
                                            Object[] args,
                                            CultureInfo culture,
                                            Object[] activationAttributes)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type is System.Reflection.Emit.TypeBuilder)
            {
                throw new NotSupportedException(SR.NotSupported_CreateInstanceWithTypeBuilder);
            }

            // If they didn't specify a lookup, then we will provide the default lookup.
            if ((bindingAttr & (BindingFlags)LookupMask) == 0)
            {
                bindingAttr |= Activator.ConstructorDefault;
            }

            if (activationAttributes != null && activationAttributes.Length > 0)
            {
                throw new PlatformNotSupportedException(SR.NotSupported_ActivAttr);
            }

            RuntimeType rt = type.UnderlyingSystemType as RuntimeType;

            if (rt == null)
            {
                throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
            }

            return(rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes));
        }