zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }


     

  • 相关阅读:
    ubuntu搭建php开发环境记录
    zz-什么是网关,路由,dns,通俗讲解
    如何设置root用户密码
    zz三台centos7虚拟机设置相互免密码登录
    go之闭包及其应用
    网络是怎样连接的
    进程间通信方式探索
    现代操作系统——操作系统概念
    现代操作系统——硬件_IO设备——设备控制器和设备本身
    simotion byte/word ASCII码转换为字符、字符串
  • 原文地址:https://www.cnblogs.com/scottckt/p/1838856.html
Copyright © 2011-2022 走看看