zoukankan      html  css  js  c++  java
  • (C#基础)反射理解

        这个知识点很基础。

       代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace dazilianxi
    {
       public class book:IComparable
        {
           private int price;
           private string title;
           public book() { }
           public book(int price ,string title)
           {
               this.price = price;
               this.title = title;
           }
           public int Price
           {
               get { return this.price; }
           }
    
           public string Title {
               get { return this.title; }
           }
    
    
           #region IComparable 成员
    
           public int CompareTo(object obj)
           {
               book mbook = (book)obj;
               return this.Price.CompareTo(mbook.Price);
           }
           public string DisplayName(string name)
           {
               return string.Format("学生姓名:{0}", name);
           }
    
           #endregion
        }
    }

    运行的main中代码:

    /*
                Type type = Type.GetType("dazilianxi.book");
                Console.WriteLine(type.Name);
                Console.WriteLine(type.FullName);
                Console.WriteLine(type.Namespace);
                //获取属性
                PropertyInfo[] info = type.GetProperties();
                foreach( PropertyInfo item in info )
                {
                    Console.WriteLine(item.Name);
                }
    
                Console.WriteLine("下面试方法");
                //获取方法
                MethodInfo[] meth = type.GetMethods();
                foreach(MethodInfo me in meth)
                {
                    Console.WriteLine(me.ReturnType.Name);
                    Console.WriteLine(me.Name);
                }
                */
                book lob = new book( 100,"好书刊");
    
                //获取运行时的程序集
                Assembly asm = Assembly.GetExecutingAssembly();
                //获取运行时的Type类型
                Type type = asm.GetType("dazilianxi.book");
    
                //获取运行时的对象实例
                object stu = Activator.CreateInstance(type);
    
                //获取运行时指定方法
                MethodInfo method = type.GetMethod("DisplayName");
                object[] parameters = new object[1];//定义参数数组
                parameters[0] = "88lll";//参数赋值
              //  parameters[1] = "hello";
                //触发运行时的方法
                string result = (string)method.Invoke(stu, parameters);//得到实例参数结果值
                Console.WriteLine(result);

     参考:http://www.cnblogs.com/darrenji/p/3817999.html

  • 相关阅读:
    01-JAVA语言基础(动手动脑)
    大道至简第一章JAVA伪代码形式读后感
    《美国教授写给被开除中国留学生的信》阅读笔记
    《大道至简》读后感
    使用java2Word生成Word文档打不开报错 存在非法字符xml
    继上篇博客对安卓爬虫以及TextView更新的问题解释
    安卓Jsoup爬虫
    AndroidStdio模拟器打不开报错 Guest isn't online after 7 seconds
    MapReduce
    水晶报表报错:log4net初始值问题
  • 原文地址:https://www.cnblogs.com/annabook/p/4972171.html
Copyright © 2011-2022 走看看