System.Reflection.EventInfo
类的EventHandlerType
属性被声明为返回可为空的Type?
:
EventInfo.EventHandlerType Property
public virtual Type? EventHandlerType { get; }
当然,任何事件都必须有类型--那么为什么返回值可以为空呢?
如果我判断实现的反汇编,我想我看到它不能返回空:
namespace System.Reflection.TypeLoading
{
/// <summary>
/// Base class for all EventInfo objects created by a MetadataLoadContext.
/// </summary>
internal abstract partial class RoEvent : LeveledEventInfo
{
public sealed override Type EventHandlerType => _lazyEventType ?? (_lazyEventType = ComputeEventHandlerType());
protected abstract Type ComputeEventHandlerType();
private volatile Type? _lazyEventType;
它与类似的属性不同,比如FieldInfo.FieldType
和MethodInfo.ReturnType
,它们返回Type
而不是Type?
.