예제 #1
0
        internal static ObjectHandle CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo, ref StackCrawlMark stackMark)
        {
            Assembly executingAssembly;

            if ((securityInfo != null) && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
            }
            if (assemblyName == null)
            {
                executingAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
            }
            else
            {
                executingAssembly = RuntimeAssembly.InternalLoad(assemblyName, securityInfo, ref stackMark, false);
            }
            if (executingAssembly == null)
            {
                return(null);
            }
            object o = CreateInstance(executingAssembly.GetType(typeName, true, ignoreCase), bindingAttr, binder, args, culture, activationAttributes);

            if (o == null)
            {
                return(null);
            }
            return(new ObjectHandle(o));
        }
예제 #2
0
        private static ObjectHandle CreateInstanceInternal(string assemblyString,
                                                           string typeName,
                                                           bool ignoreCase,
                                                           BindingFlags bindingAttr,
                                                           Binder binder,
                                                           object[] args,
                                                           CultureInfo culture,
                                                           object[] activationAttributes,
                                                           ref StackCrawlMark stackMark)
        {
            Type     type     = null;
            Assembly assembly = null;

            if (assemblyString == null)
            {
                assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
            }
            else
            {
                RuntimeAssembly assemblyFromResolveEvent;
                AssemblyName    assemblyName = RuntimeAssembly.CreateAssemblyName(assemblyString, out assemblyFromResolveEvent);
                if (assemblyFromResolveEvent != null)
                {
                    // Assembly was resolved via AssemblyResolve event
                    assembly = assemblyFromResolveEvent;
                }
                else if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
                {
                    // WinRT type - we have to use Type.GetType
                    type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
                }
                else
                {
                    // Classic managed type
                    assembly = RuntimeAssembly.InternalLoadAssemblyName(
                        assemblyName, null, ref stackMark,
                        true /*thrownOnFileNotFound*/);
                }
            }

            if (type == null)
            {
                // It's classic managed type (not WinRT type)
                if (assembly == null)
                {
                    return(null);
                }

                type = assembly.GetType(typeName, true /*throwOnError*/, ignoreCase);
            }

            object o = Activator.CreateInstance(type,
                                                bindingAttr,
                                                binder,
                                                args,
                                                culture,
                                                activationAttributes);

            return((o != null) ? new ObjectHandle(o) : null);
        }
예제 #3
0
        internal static Delegate CreateDelegateInternal(RuntimeType rtType, RuntimeMethodInfo rtMethod, Object firstArgument, DelegateBindingFlags flags, ref StackCrawlMark stackMark)
        {
            Contract.Assert((flags & DelegateBindingFlags.SkipSecurityChecks) == 0);

#if FEATURE_APPX
            bool nonW8PMethod = (rtMethod.InvocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0;
            bool nonW8PType   = (rtType.InvocationFlags & INVOCATION_FLAGS.INVOCATION_FLAGS_NON_W8P_FX_API) != 0;
            if (nonW8PMethod || nonW8PType)
            {
                RuntimeAssembly caller = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                if (caller != null && !caller.IsSafeForReflection())
                {
                    throw new InvalidOperationException(
                              Environment.GetResourceString("InvalidOperation_APIInvalidForCurrentContext",
                                                            nonW8PMethod ? rtMethod.FullName : rtType.FullName));
                }
            }
#endif

            return(UnsafeCreateDelegate(rtType, rtMethod, firstArgument, flags));
        }
예제 #4
0
        static internal ObjectHandle CreateInstance(String assemblyString,
                                                    String typeName,
                                                    bool ignoreCase,
                                                    BindingFlags bindingAttr,
                                                    Binder binder,
                                                    Object[] args,
                                                    CultureInfo culture,
                                                    Object[] activationAttributes,
                                                    Evidence securityInfo,
                                                    ref StackCrawlMark stackMark)
        {
            Type     type     = null;
            Assembly assembly = null;

            if (assemblyString == null)
            {
                assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
            }
            else
            {
                RuntimeAssembly assemblyFromResolveEvent;
                AssemblyName    assemblyName = RuntimeAssembly.CreateAssemblyName(assemblyString, false /*forIntrospection*/, out assemblyFromResolveEvent);
                if (assemblyFromResolveEvent != null)
                {
                    // Assembly was resolved via AssemblyResolve event
                    assembly = assemblyFromResolveEvent;
                }
                else if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
                {
                    // WinRT type - we have to use Type.GetType
                    type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
                }
                else
                {
                    // Classic managed type
                    assembly = RuntimeAssembly.InternalLoadAssemblyName(
                        assemblyName, securityInfo, null, ref stackMark,
                        true /*thrownOnFileNotFound*/, false /*forIntrospection*/);
                }
            }

            if (type == null)
            {
                // It's classic managed type (not WinRT type)
                Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyString);
                if (assembly == null)
                {
                    return(null);
                }

                type = assembly.GetType(typeName, true /*throwOnError*/, ignoreCase);
            }

            Object o = Activator.CreateInstance(type,
                                                bindingAttr,
                                                binder,
                                                args,
                                                culture,
                                                activationAttributes);

            Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
            if (o == null)
            {
                return(null);
            }
            else
            {
                ObjectHandle Handle = new ObjectHandle(o);
                return(Handle);
            }
        }
예제 #5
0
        [System.Security.SecurityCritical]  // auto-generated
        static internal ObjectHandle CreateInstance(String assemblyString,
                                                    String typeName,
                                                    bool ignoreCase,
                                                    BindingFlags bindingAttr,
                                                    Binder binder,
                                                    Object[] args,
                                                    CultureInfo culture,
                                                    Object[] activationAttributes,
                                                    Evidence securityInfo,
                                                    ref StackCrawlMark stackMark)
        {
#if FEATURE_CAS_POLICY
            if (securityInfo != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled)
            {
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit"));
            }
#endif // FEATURE_CAS_POLICY
            Type     type     = null;
            Assembly assembly = null;
            if (assemblyString == null)
            {
                assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
            }
            else
            {
                RuntimeAssembly assemblyFromResolveEvent;
                AssemblyName    assemblyName = RuntimeAssembly.CreateAssemblyName(assemblyString, false /*forIntrospection*/, out assemblyFromResolveEvent);
                if (assemblyFromResolveEvent != null)
                {
                    // Assembly was resolved via AssemblyResolve event
                    assembly = assemblyFromResolveEvent;
                }
                else if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
                {
                    // WinRT type - we have to use Type.GetType
                    type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
                }
                else
                {
                    // Classic managed type
                    assembly = RuntimeAssembly.InternalLoadAssemblyName(
                        assemblyName, securityInfo, null, ref stackMark,
                        true /*thrownOnFileNotFound*/, false /*forIntrospection*/, false /*suppressSecurityChecks*/);
                }
            }

            if (type == null)
            {
                // It's classic managed type (not WinRT type)
                Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyString);
                if (assembly == null)
                {
                    return(null);
                }

                type = assembly.GetType(typeName, true /*throwOnError*/, ignoreCase);
            }

            Object o = Activator.CreateInstance(type,
                                                bindingAttr,
                                                binder,
                                                args,
                                                culture,
                                                activationAttributes);

            Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
            if (o == null)
            {
                return(null);
            }
            else
            {
                ObjectHandle Handle = new ObjectHandle(o);
                return(Handle);
            }
        }