zoukankan      html  css  js  c++  java
  • C#基础知识回顾-- 反射(3)

     

    获取Type对象的构造函数:

      前一篇因为篇幅问题因为篇幅太短被移除首页,反射这一块还有一篇“怎样在程序集中使用反射”,

    其他没有什么可以写的了,前两篇主要是铺垫,在实际应用中,主要是最后两篇。其实写完反射,

    我也考虑重新把委托重新写下,现在想想确实太过单一和简单了。我会把实际开发中委托的例子多写几个

    , 以便以理解。如果这几篇C#的基础对您有所帮助的话,请点击推荐。谢谢您的支持……

      好了,不废话了,在前面例子中,由于MyClass类型的对象是显示创建的,因此使

    用反射技术来调用MyClass上的方法没有任何优势--以普通的方式调用对象上的方法会简单的多。但是,

    如果对象是在运行时动态创建的,反射的功能就显示出来了。在这种情况下,需要首先获取一个构造函

    数列表,然后再调用列表中的某个构造函数,创建一个该类型的实例。通过这种机制,可以在运行时实例

    化任意类型的对象而不必在声明中指定。

      为了获得某个类型的构造函数,需要调用Type对象上的GetConstructors()。常用形式为:

      ConstructorInfo[] GetConstructors()

      该方法返回一个描述构造函数的ConstructorInfo对象数组。ConstructorInfo中常用的

    是GetParamters()方法,该方法返回给定构造函数的参数列表。

      一旦找到了合适的构造函数,就调用ConstructorInfo定义的Invoke()方法来创建对象:

      object Invoke(object[] args)

     

      需要传递给此方法的所有参数都在args中指定。如果不需要参数,args必须为null。另外,

     

    args必须包含与参数个数相同的元素,并且实参的类型必须与形参的类型兼容。Invoke()方法返回

    的是指向新构造对象的引用。

    例子:

    测试对象类

    class MyClass
    {
        int x;
        int y;
    
        public MyClass(int i)
        {
            Console.WriteLine("一个参数的构造函数:");
            x = y = i;
        }
        public MyClass(int i, int j)
        {
            Console.WriteLine("两个参数构造函数:");
            x = i;
            y = j;
    
            Show();
        }
    
        public int Sum()
        {
            return x + y;
        }
    
        public bool IsBetween(int i)
        {
            if (x < i && i < y)
                return true;
            else
                return false;
        }
    
        public void Set(int a, int b)
        {
            Console.Write("函数:Set(int a, int b)");
            x = a;
            y = b;
    
            Show();
        }
    
        public void Set(double a, double b)
        {
            Console.Write("函数:Set(double a, double b)");
    
            x = (int)a;
            y = (int)b;
    
            Show();
        }
    
        public void Show()
        {
            Console.WriteLine("x:{0},y:{1}", x, y);
        }
    }

    使用反射:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    namespace Reflection
    {
        class Program
        {
            static void Main(string[] args)
            {
                InvokeConsDemo();
                Console.ReadKey();
            }
    
          
    
            static void InvokeConsDemo()
            {
    
                Type t = typeof(MyClass);
    
                int val;
    
                ConstructorInfo[] ci = t.GetConstructors();
    
                Console.WriteLine("类构造函数如下:");
    
                foreach (ConstructorInfo c in ci)
                {
                    Console.Write("" + t.Name + "(");
    
                    ParameterInfo[] pi = c.GetParameters();
    
                    for (int i = 0; i < pi.Length; i++)
                    {
                        Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name);
    
                        if (i + 1 < pi.Length) Console.Write(", ");
                    }
                    Console.WriteLine(") ");
    
                }
                Console.WriteLine();
    
                int x;
    
                for (x = 0; x < ci.Length; x++)
                {
                    ParameterInfo[] pi = ci[x].GetParameters();
    
                    if (pi.Length == 2) break;
                }
    
                if (x == ci.Length)
                {
                    Console.WriteLine("没有找到两个参数的构造函数"); return;
                }
                else
                {
                    object[] consargs = new object[2];
    
                    consargs[0] = 10;
                    consargs[1] = 20;
    
                    object reflectOb = ci[x].Invoke(consargs);
    
                    Console.WriteLine("用reflectOb调用方法");
    
                    Console.WriteLine();
    
                    MethodInfo[] mi = t.GetMethods();
    
                    foreach (MethodInfo m in mi)
                    {
                        ParameterInfo[] pi = m.GetParameters();
    
                        if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(int))
                        {
                            object[] args = new object[2];
    
                            args[0] = 12;
                            args[1] = 7;
    
                            m.Invoke(reflectOb, args);
                        }
                        else if (m.Name.CompareTo("Set") == 0 && pi[0].ParameterType == typeof(double))
                        {
                            object[] args = new object[2];
    
                            args[0] = 1.25;
                            args[1] = 7.5;
    
                            m.Invoke(reflectOb, args);
                        }
                        else if (m.Name.CompareTo("Sum") == 0)
                        {
                            val = (int)m.Invoke(reflectOb, null);
    
                            Console.WriteLine("Sum is {0}",val);
                        }
                        else if (m.Name.CompareTo("IsBetween") == 0)
                        {
                            object[] args = new object[1];
    
                            args[0] = 13;
    
                            if ((bool)m.Invoke(reflectOb, args))
                            {
                                Console.WriteLine("13 is between x and y");
                            }
    
                        }
                        else if (m.Name.CompareTo("Show") == 0)
                        {
                            m.Invoke(reflectOb, null);
                        }
                    }
    
                }
            }
        }
    }

    运行结果为:

     

      本例中,找到了一个两个参数的构造函数,那么使用下面的语句实例化了一个该类型的对象:

     

    object reflectOb=ci[x].Invoke(consargs);

     

    调用Invoke()方法后,reflectOb将引用一个MyClass类型的对象。此后,程序将执行

     

    reflectOb上的方法。

     

    注意:本例为了简单起见,假设了一个使用两个参数的构造函数,并且两个参数都为int类型

     

    。但在实际的应用程序中,必须检验每一个参数的类型。

     

  • 相关阅读:
    .net 使用 swagger 操作
    oracle 基础
    关于webapi跨域问题的一些坑坑
    sqlHelper
    微博数据接入开发
    mvc后台上传
    sql server 报错处理
    asp.net 微信开发(二)
    收集一些关于OI/ACM的奇怪的东西……
    LeetCode 229 Majority Element II
  • 原文地址:https://www.cnblogs.com/smiler/p/3179105.html
Copyright © 2011-2022 走看看