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

SecureCreateInstance() static private méthode

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

            // 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 && !(type.IsPublic || type.IsNestedPublic)) {
                (new ReflectionPermission(PermissionState.Unrestricted)).Demand();
            }

            return Activator.CreateInstance(type, args);
        }

Same methods

SecurityUtils::SecureCreateInstance ( Type type ) : object
SecurityUtils::SecureCreateInstance ( Type type, object args, bool allowNonPublic ) : 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