• MCPD 70536题目 反射


      You use Reflection to obtain information about a method named MyMethod. You need to ascertain whether MyMethod is accessible to a derived class. What should you do?

      使用反射得到方法MyMethod的信息。你需要确定MyMethod是否是可以应用于派生类。你要怎么做?

     

      ACall the IsAssembly property of the MethodInfo class.

      BCall the IsVirtual property of the MethodInfo class.

      CCall the IsStatic property of the MethodInfo class.

      DCall the IsFamily property of the MethodInfo class. 

    答案:D。

      注意:A:是在程序集外的不可见。D:只要是派生类就可见

    解:

      IsAssembly:此方法或构造函数只对同一程序集中的其他类型可见,而对该程序集以外的派生类型则不可见。
      IsFamily:此方法或构造函数仅在其类和派生类内可见。
      IsVirtual:该值指示方法是否为 virtual
      IsStatic:该值指示方法是否为 static

    各定义对应的值:

                                  IsAssembly        IsFamilyOrAssembly
                         IsPublic            IsFamily          IsFamilyAndAssembly

    public               True       False    False    False    False
    internal             False      True     False    False    False
    protected            False      False    True     False    False
    protected internal    False      False    False    True     False

    测试代码:

      新建两个控制台项目,如下图:

       在ReflectMCPD中编写下边代码。此类主要是通过反射得到方法的集息。

    using System.Reflection;

    namespace ReflectMCPD
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                System.Reflection.MemberInfo[] childMemberInfo 
    = typeof(Father).GetMembers(
                    BindingFlags.Instance 
    |
                    BindingFlags.NonPublic 
    |
                    BindingFlags.Public);
                
    foreach (MemberInfo infoItem in childMemberInfo)
                {
                    
    switch (infoItem.MemberType)
                    {
                        
    case MemberTypes.All:
                            
    break;
                        
    case MemberTypes.Constructor:
                            
    break;
                        
    case MemberTypes.Custom:
                            
    break;
                        
    case MemberTypes.Event:
                            
    break;
                        
    case MemberTypes.Field:
                            
    break;
                        
    case MemberTypes.Method:
                            MethodInfo methodItemInfo 
    = typeof(Father).GetMethod(infoItem.Name,
                                BindingFlags.Instance 
    |
                                BindingFlags.NonPublic 
    |
                                BindingFlags.Public);
                            
    if (methodItemInfo != null)
                            {
                                Console.WriteLine(
    "方法名:{0},同一程序集可见:{1},类及派生类可见:{2}",
                                    methodItemInfo.Name, methodItemInfo.IsAssemblymethodItemInfo.IsFamily);
                            }
                            
    break;
                        
    case MemberTypes.NestedType:
                            
    break;
                        
    case MemberTypes.Property:
                            
    break;
                        
    case MemberTypes.TypeInfo:
                            
    break;
                        
    default:
                            
    break;
                    }
                }
                Console.ReadKey();
            }
        }

        
    /// <summary>
        
    /// 父类
        
    /// </summary>
        public class Father
        {
            
    public string MyMethod()
            {           
                
    return "测试";
            }

            
    internal void InternalMethod()
            {
            }

            
    protected void ProtectedMetod()
            {
            }

            
    protected internal void ProtectedInternalMetod()
            {
            }
        }
    }

      在OtherReflect引用ReflectMCPD。编写如下代码。此类主要为了测试internal和protected在程序集以外的情况。

    using ReflectMCPD;

    namespace OtherReflect
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {            
            }
        }

        
    public class SubChild : ReflectMCPD.Father
        {
            
    public void SubChildMethod()
            {            
                ProtectedMetod();
                ProtectedInternalMetod();
            }
        }
    }


     

  • 相关阅读:
    2019沈阳网路赛 D. Fish eating fruit (点分治)
    2019南京网路赛 A.The beautiful values of the palace (主席树)
    洛谷 P2634 [国家集训队]聪聪可可(点分治)
    AcWing252 树 (点分治模板题)
    点分治模板 (洛谷 P3806)
    2020牛客寒假算法基础集训营2 J-求函数(线段树维护矩阵乘法)
    七夕祭(贪心+中位数)
    数据结构-集合
    数据结构-广义表
    数据结构-稀疏矩阵
  • 原文地址:https://www.cnblogs.com/scottckt/p/1838856.html
Copyright © 2020-2023  润新知