public static object CreateInstance(Type type, bool nonPublic) { CheckType(type); #if NET_2_0 if (type.ContainsGenericParameters) { throw new ArgumentException(type + " is an open generic type", "type"); } #endif CheckAbstractType(type); ConstructorInfo ctor; MonoType monoType = type as MonoType; if (monoType != null) { ctor = monoType.GetDefaultConstructor(); if (!nonPublic && ctor != null && !ctor.IsPublic) { ctor = null; } } else { BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; if (nonPublic) { flags |= BindingFlags.NonPublic; } ctor = type.GetConstructor(flags, null, CallingConventions.Any, Type.EmptyTypes, null); } if (ctor == null) { if (type.IsValueType) { return(CreateInstanceInternal(type)); } throw new MissingMethodException(Locale.GetText("Default constructor not found."), ".ctor() of " + type.FullName); } return(ctor.Invoke(null)); }
public static object CreateInstance(Type type, bool nonPublic) { CheckType(type); if (type.ContainsGenericParameters) { throw new ArgumentException(type + " is an open generic type", "type"); } MonoType monoType = type.UnderlyingSystemType as MonoType; if (monoType == null) { throw new ArgumentException("Type must be a type provided by the runtime"); } CheckAbstractType(monoType); var ctor = monoType.GetDefaultConstructor(); if (!nonPublic && ctor != null && !ctor.IsPublic) { ctor = null; } if (ctor == null) { if (type.IsValueType) { return(CreateInstanceInternal(type)); } throw new MissingMethodException(Locale.GetText("Default constructor not found for type " + type.FullName)); } return(ctor.InternalInvoke(null, null)); }