private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
{
var rt = (RuntimeType)structureType;
- object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, wrapExceptions: true);
+ object structure = rt.CreateInstanceDefaultCtor(publicOnly: false, skipCheckThis: false, fillCache: false, wrapExceptions: true)!;
PtrToStructureHelper(ptr, structure, allowValueClasses: true);
return structure;
}
throw new NotSupportedException(SR.Acc_CreateVoid);
}
- internal object CreateInstanceImpl(
+ internal object? CreateInstanceImpl(
BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
{
CreateInstanceCheckThis();
- object server;
+ object? instance;
args ??= Array.Empty<object>();
if (args.Length == 0 && (bindingAttr & BindingFlags.Public) != 0 && (bindingAttr & BindingFlags.Instance) != 0
&& (IsGenericCOMObjectImpl() || IsValueType))
{
- server = CreateInstanceDefaultCtor(publicOnly, false, true, wrapExceptions);
+ instance = CreateInstanceDefaultCtor(publicOnly, skipCheckThis: false, fillCache: true, wrapExceptions);
}
else
{
}
// fast path??
- server = Activator.CreateInstance(this, nonPublic: true, wrapExceptions: wrapExceptions)!;
+ instance = Activator.CreateInstance(this, nonPublic: true, wrapExceptions: wrapExceptions);
}
else
{
- server = ((ConstructorInfo)invokeMethod).Invoke(bindingAttr, binder, args, culture);
+ instance = ((ConstructorInfo)invokeMethod).Invoke(bindingAttr, binder, args, culture);
if (state != null)
binder.ReorderArgumentArray(ref args, state);
}
}
- return server;
+ return instance;
}
// the cache entry
/// <summary>
/// The slow path of CreateInstanceDefaultCtor
/// </summary>
- private object CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)
+ private object? CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)
{
RuntimeMethodHandleInternal runtimeCtor = default;
bool canBeCached = false;
/// </summary>
[DebuggerStepThrough]
[DebuggerHidden]
- internal object CreateInstanceDefaultCtor(bool publicOnly, bool skipCheckThis, bool fillCache, bool wrapExceptions)
+ internal object? CreateInstanceDefaultCtor(bool publicOnly, bool skipCheckThis, bool fillCache, bool wrapExceptions)
{
// Call the cached
if (GenericCache is ActivatorCache cacheEntry)
{
public static partial class Activator
{
+ //
+ // Note: CreateInstance returns null for Nullable<T>, e.g. CreateInstance(typeof(int?)) returns null.
+ //
+
public static object? CreateInstance(Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes)
{
if (type is null)
[System.Runtime.CompilerServices.Intrinsic]
public static T CreateInstance<T>()
{
- return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true);
+ return (T)((RuntimeType)typeof(T)).CreateInstanceDefaultCtor(publicOnly: true, skipCheckThis: true, fillCache: true, wrapExceptions: true)!;
}
private static T CreateDefaultInstance<T>() where T : struct => default;