System.Windows.Forms.SecurityUtils.SecureCreateInstance C# (CSharp) Méthode

SecureCreateInstance() static private méthode

static private SecureCreateInstance ( Type type, object args, bool allowNonPublic ) : object
type System.Type
args object
allowNonPublic bool
Résultat object
        internal static object SecureCreateInstance(Type type, object[] args, bool allowNonPublic) {
            if (type == null) {
                throw new ArgumentNullException("type");
            }

            BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;

            // The only case we need to worry about is when the type is in the same assembly
            // as us. In all other cases, reflection will take care of security.
            if (type.Assembly == typeof(SecurityUtils).Assembly) {
                // if it's an internal type, we demand reflection permission.
                if (!(type.IsPublic || type.IsNestedPublic)) {
                    (new ReflectionPermission(PermissionState.Unrestricted)).Demand();
                }
                else if (allowNonPublic && !HasReflectionPermission) {
                    // Someone is trying to instantiate a public type in *our* assembly, but does not
                    // have full reflection permission. We shouldn't pass BindingFlags.NonPublic in this case.
                    // The reason we don't directly demand the permission here is because we don't know whether
                    // a public nr non-public .ctor will be invoked. We want to allow the public .ctor case to
                    // succeed.
                    allowNonPublic = false;
                }                
            }

            if (allowNonPublic) {
                flags |= BindingFlags.NonPublic;
            }

            return Activator.CreateInstance(type, flags, null, args, null);
        }

Same methods

SecurityUtils::SecureCreateInstance ( Type type ) : object
SecurityUtils::SecureCreateInstance ( Type type, object args ) : object

Usage Example

        // Create an object of the given type. Throw an exception if this fails.
        private static object CreateInstanceOfType(Type type)
        {
            object    instancedObject   = null;
            Exception instanceException = null;

            try {
                instancedObject = SecurityUtils.SecureCreateInstance(type);
            }
            catch (TargetInvocationException ex) {
                instanceException = ex; // Default ctor threw an exception
            }
            catch (MethodAccessException ex) {
                instanceException = ex; // Default ctor was not public
            }
            catch (MissingMethodException ex) {
                instanceException = ex; // No default ctor defined
            }

            if (instanceException != null)
            {
                throw new NotSupportedException(SR.GetString(SR.BindingSourceInstanceError), instanceException);
            }

            return(instancedObject);
        }
All Usage Examples Of System.Windows.Forms.SecurityUtils::SecureCreateInstance