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));
}