dotnet/reactive · error · ArgumentNullException
nameof(type)
Error message
nameof(type)
What it means
This is an ArgumentNullException raised by the static-event overload FromEventPattern(Type type, string eventName, IAsyncScheduler scheduler) when the type argument is null. This overload subscribes to static .NET events via reflection on the given Type, so a null Type cannot be reflected over and is rejected synchronously before any observable is created.
Solutions
- Use typeof(SenderType) instead of a string-based Type lookup to get a compile-time-guaranteed non-null Type
- If using Type.GetType, verify the assembly-qualified name and check the result for null before calling FromEventPattern
- Ensure plugin/reference assemblies are present so the type actually resolves
Example fix
// before
var t = Type.GetType("MyApp.Services.Messenger"); // null: wrong namespace
var events = AsyncObservable.FromEventPattern(t, "MessageReceived");
// after
var events = AsyncObservable.FromEventPattern(typeof(MyApp.Services.Messenger), "MessageReceived"); Defensive patterns
Strategy: validation
Validate before calling
if (type is null)
throw new ArgumentNullException(nameof(type), "Event source type not resolved");
var obs = AsyncObservable.FromEventPattern(type, eventName); Type guard
static bool HasSourceType([NotNullWhen(true)] Type? t) => t is not null;
Try / catch
try
{
var obs = AsyncObservable.FromEventPattern(type, eventName);
}
catch (ArgumentNullException ex) when (ex.ParamName == "type")
{
// string-based Type lookup failed; log the bad type name
throw new TypeLoadException($"Could not resolve event source type '{typeByName}'", ex);
} Prevention
- Prefer typeof(X) over Type.GetType strings
- When Type.GetType is required, use assembly-qualified names and null-check the result
- Verify plugin assemblies load before reflecting their types
- Unit-test dynamic type resolution for every configured type name
When it happens
Trigger: Calling the public FromEventPattern(Type type, string eventName) or explicit-scheduler overload (AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs:139) with type == null, e.g. passing the result of Type.GetType("Bad.Namespace.Type") which returns null when the type name/assembly is wrong.
Common situations: Type.GetType with a mistyped or assembly-unqualified name returning null; loading types from plugins where the assembly failed to load; reflection-based registration code that forwards a null type it obtained elsewhere.
Related errors
- Value cannot be null. (Parameter 'scheduler')
- Value cannot be null. (Parameter 'source')
- Value cannot be null.
- addHandler
- removeHandler
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/ef83c4440c17292a.
Report an issue: GitHub.
Appendix: source
Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs:139
public static IAsyncObservable<EventPattern<TSender, TEventArgs>> FromEventPattern<TSender, TEventArgs>(object target, string eventName, IAsyncScheduler scheduler)
{
if (target == null)
throw new ArgumentNullException(nameof(target));
if (eventName == null)
throw new ArgumentNullException(nameof(eventName));
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
return FromEventPatternCore<TSender, TEventArgs, EventPattern<TSender, TEventArgs>>(target.GetType(), target, eventName, scheduler, (o, e) => new EventPattern<TSender, TEventArgs>(o, e));
}
public static IAsyncObservable<EventPattern<object>> FromEventPattern(Type type, string eventName) => FromEventPattern(type, eventName, GetSchedulerForCurrentContext());
public static IAsyncObservable<EventPattern<object>> FromEventPattern(Type type, string eventName, IAsyncScheduler scheduler)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (eventName == null)
throw new ArgumentNullException(nameof(eventName));
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));
return FromEventPatternCore<object, object, EventPattern<object>>(type, null, eventName, scheduler, (o, e) => new EventPattern<object>(o, e));
}
public static IAsyncObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Type type, string eventName) => FromEventPattern<TEventArgs>(type, eventName, GetSchedulerForCurrentContext());
public static IAsyncObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(Type type, string eventName, IAsyncScheduler scheduler)
{
if (type == null)
throw new ArgumentNullException(nameof(type));
if (eventName == null)
throw new ArgumentNullException(nameof(eventName));
if (scheduler == null)
throw new ArgumentNullException(nameof(scheduler));View on GitHub (pinned to 94b5d5ab91)