(C#基础)反射理解
发布日期:2022-03-11 15:03:36 浏览次数:9 分类:技术文章

本文共 2231 字,大约阅读时间需要 7 分钟。

    这个知识点很基础。

   代码

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

转载于:https://www.cnblogs.com/annabook/p/4972171.html

转载地址:https://blog.csdn.net/weixin_30267785/article/details/99169413 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:JS/Jquery版本的俄罗斯方块(附源码分析)
下一篇:关于javaScript注册事件传递参数的浅析

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月03日 08时21分07秒