为什么允许从 MemberInfo 转换为 MethodInfo?
Why cast from MemberInfo to MethodInfo is allowed?
System.Reflection.MemberInfo 的文档有以下示例:
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.MemberType == MemberTypes.Method)
{
foreach (ParameterInfo pi in ((MethodInfo)mi).GetParameters())
注意演员表 (MethodInfo)mi)
。 MemberInfo
和 MethodInfo
没有共同的父级,那么为什么允许转换?
MethodInfo
继承自 MethodBase
继承自 MemberInfo
.
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public abstract class MethodInfo : MethodBase,
_MethodInfo
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public abstract class MethodBase : MemberInfo,
_MethodBase
MethodInfo
is derived from MethodBase
, which is derived from MemberInfo
。请注意,封闭 if
中的 mi.MemberType == MemberTypes.Method
条件会在运行时防止 InvalidCastException
发生。
System.Reflection.MemberInfo 的文档有以下示例:
foreach (MemberInfo mi in t.GetMembers())
{
if (mi.MemberType == MemberTypes.Method)
{
foreach (ParameterInfo pi in ((MethodInfo)mi).GetParameters())
注意演员表 (MethodInfo)mi)
。 MemberInfo
和 MethodInfo
没有共同的父级,那么为什么允许转换?
MethodInfo
继承自 MethodBase
继承自 MemberInfo
.
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public abstract class MethodInfo : MethodBase,
_MethodInfo
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
[PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")]
public abstract class MethodBase : MemberInfo,
_MethodBase
MethodInfo
is derived from MethodBase
, which is derived from MemberInfo
。请注意,封闭 if
中的 mi.MemberType == MemberTypes.Method
条件会在运行时防止 InvalidCastException
发生。