[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable static public T CreateInstance <T>() { RuntimeType rt = typeof(T) as RuntimeType; // This is a hack to maintain compatibility with V2. Without this we would throw a NotSupportedException for void[]. // Array, Ref, and Pointer types don't have default constructors. if (rt.HasElementType) { throw new MissingMethodException(Environment.GetResourceString("Arg_NoDefCTor")); } StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; // Skip the CreateInstanceCheckThis call to avoid perf cost and to maintain compatibility with V2 (throwing the same exceptions). #if FEATURE_CORECLR // In SL2/3 CreateInstance<T> doesn't do any security checks. This would mean that Assembly B can create instances of an internal // type in Assembly A upon A's request: // TypeInAssemblyA.DoWork() { AssemblyB.Create<InternalTypeInAssemblyA>();} // TypeInAssemblyB.Create<T>() {return new T();} // This violates type safety but we saw multiple user apps that have put a dependency on it. So for compatability we allow this if // the SL app was built against SL2/3. // Note that in SL2/3 it is possible for app code to instantiate public transparent types with public critical default constructors. // Fortunately we don't have such types in out platform assemblies. if (CompatibilitySwitches.IsAppEarlierThanSilverlight4 || CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { return((T)rt.CreateInstanceSlow(true /*publicOnly*/, true /*skipCheckThis*/, false /*fillCache*/, ref stackMark)); } else #endif // FEATURE_CORECLR return((T)rt.CreateInstanceDefaultCtor(true /*publicOnly*/, true /*skipCheckThis*/, true /*fillCache*/, true /*wrapExceptions*/, ref stackMark)); }
public static T CreateInstance <T>() { RuntimeType type = typeof(T) as RuntimeType; if (type.HasElementType) { throw new MissingMethodException(Environment.GetResourceString("Arg_NoDefCTor")); } return((T)type.CreateInstanceDefaultCtor(true, true, true, true)); }
public static object CreateInstance(Type type, bool nonPublic) { if (type == null) { throw new ArgumentNullException("type"); } RuntimeType underlyingSystemType = type.UnderlyingSystemType as RuntimeType; if (underlyingSystemType == null) { throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type"); } return(underlyingSystemType.CreateInstanceDefaultCtor(!nonPublic)); }
static public T CreateInstance <T>() { RuntimeType rt = typeof(T) as RuntimeType; // This is a workaround to maintain compatibility with V2. Without this we would throw a NotSupportedException for void[]. // Array, Ref, and Pointer types don't have default constructors. if (rt.HasElementType) { throw new MissingMethodException(SR.Arg_NoDefCTor); } // Skip the CreateInstanceCheckThis call to avoid perf cost and to maintain compatibility with V2 (throwing the same exceptions). return((T)rt.CreateInstanceDefaultCtor(true /*publicOnly*/, true /*skipCheckThis*/, true /*fillCache*/, true /*wrapExceptions*/)); }
[MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable static public Object CreateInstance(Type type, bool nonPublic) { if ((object)type == null) throw new ArgumentNullException("type"); Contract.EndContractBlock(); RuntimeType rt = type.UnderlyingSystemType as RuntimeType; if (rt == null) throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type"); StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return rt.CreateInstanceDefaultCtor(!nonPublic, false, true, ref stackMark); }
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod static public T CreateInstance <T>() { RuntimeType rt = typeof(T) as RuntimeType; // This is a workaround to maintain compatibility with V2. Without this we would throw a NotSupportedException for void[]. // Array, Ref, and Pointer types don't have default constructors. if (rt.HasElementType) { throw new MissingMethodException(Environment.GetResourceString("Arg_NoDefCTor")); } StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; // Skip the CreateInstanceCheckThis call to avoid perf cost and to maintain compatibility with V2 (throwing the same exceptions). return((T)rt.CreateInstanceDefaultCtor(true /*publicOnly*/, true /*skipCheckThis*/, true /*fillCache*/, ref stackMark)); }
static internal Object CreateInstance(Type type, bool nonPublic, bool wrapExceptions) { if ((object)type == null) { throw new ArgumentNullException(nameof(type)); } RuntimeType rt = type.UnderlyingSystemType as RuntimeType; if (rt == null) { throw new ArgumentException(SR.Arg_MustBeType, nameof(type)); } return(rt.CreateInstanceDefaultCtor(!nonPublic, false, true, wrapExceptions)); }
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod static public Object CreateInstance(Type type, bool nonPublic) { if ((object)type == null) { throw new ArgumentNullException(nameof(type)); } Contract.EndContractBlock(); RuntimeType rt = type.UnderlyingSystemType as RuntimeType; if (rt == null) { throw new ArgumentException(SR.Arg_MustBeType, nameof(type)); } StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return(rt.CreateInstanceDefaultCtor(!nonPublic, false, true, ref stackMark)); }