为什么允许从MemberInfo转换为MethodInfo?

时间:2015-05-15 20:22:51

标签: c# .net reflection system.reflection

System.Reflection的文档。MemberInfo有以下示例:

foreach (MemberInfo mi in t.GetMembers())  
{
    if (mi.MemberType == MemberTypes.Method)
    {
        foreach (ParameterInfo pi in ((MethodInfo)mi).GetParameters())

注意投射(MethodInfo)mi)MemberInfoMethodInfo没有共同的父母,为什么允许投票?

2 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

MethodInfo源自MethodBase,源自MemberInfo。请注意,封闭mi.MemberType == MemberTypes.Method中的if条件在运行时来自InvalidCastException