C#学习笔记(十):反射
反射
放射是指在程序运行时动态的获取类的信息的机制,我们下面来看看C#中的反射。
Type
Type 为 System.Reflection 功能的根,也是访问元数据的主要方式。 使用 Type 的成员获取关于类型声明的信息,如构造函数、方法、字段、属性 (Property) 和类的事件,以及在其中部署该类的模块和程序集。
我们获取一个指定类型的Type有三种方法:
- 通过typeof直接获取;
- 通过调用GetType方法获取;
- 通过调用Type的静态方法GetType获取;
这三个方法都可以获取指定类型的Type对象,但是也有一点区别,具体可以参考下面两篇博客:
http://www.studyofnet.com/news/284.html
http://blog.csdn.net/letianok/article/details/7257117
下面我们来看一个例子:
using System; namespace Study
{
class Program
{
static void Main(string[] args)
{
Type t1 = typeof(string);
Console.WriteLine(t1.FullName);
//System.String Type t2 = "hello type!".GetType();
Console.WriteLine(t2.FullName);
//System.String Type t3 = Type.GetType("System.String", false);
Console.WriteLine(t3.FullName);
//System.String Console.Read();
}
}
}
获取类的信息
我们可以通过Type类型获取到类的所有信息,比如我们要获取类的所有方法信息的话,可以使用下面的代码:
using System;
using System.Reflection; namespace Study
{
class Program
{
static void Main(string[] args)
{
string str = "Hello Type!"; Type t = str.GetType();
//获取所有方法信息
MethodInfo[] methodInfos = t.GetMethods();
for (int i = ; i < methodInfos.Length; i++)
{
Console.WriteLine(methodInfos[i].Name);
}
//仅获取非静态的公共方法信息
methodInfos = t.GetMethods(BindingFlags.Instance | BindingFlags.Public);
for (int i = ; i < methodInfos.Length; i++)
{
Console.WriteLine(methodInfos[i].Name);
} Console.Read();
}
}
}
更多信息的获取方法可以参考帮助文档(https://msdn.microsoft.com/zh-cn/library/system.type(v=vs.110).aspx)。
调用指定的方法
下面我们看看如何获取一个类的指定方法并进行调用:
using System;
using System.Reflection; namespace Study
{
class Program
{
static void Main(string[] args)
{
string str = "Hello Type!"; Type t = str.GetType();
//对于存在多个重载的方法需要指明参数的类型
MethodInfo method = t.GetMethod("Split", new []{typeof(char[])});
//调用方法
string[] strs = method.Invoke(str, new object[] {new[] {' '}}) as string[];
for (int i = ; i < strs.Length; i++)
{
Console.WriteLine(strs[i]);
} Console.Read();
}
}
}
Assembly
程序集可以看做一个或多个DLL或EXE文件的集合,可以通过程序集将重复的代码提取出来,可以重复使用。
我们首先创建一个类库项目,代码如下:
namespace StudyLib
{
public class MyClass
{
public int Add(int a, int b)
{
return a + b;
} public int Add(int a, int b, int c)
{
return a + b + c;
}
}
}
然后点击菜单栏-》生成-》生成XXX,可以获得对应的DLL文件,接下来我们使用Assembly读取DLL并执行其中的方法:
using System;
using System.Reflection; namespace Study
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.LoadFrom("E:\\study\\C#\\StudyLib\\StudyLib\\bin\\Debug\\StudyLib.dll"); //输出所有类型名称
Type[] types = assembly.GetTypes();
for (int i = ; i < types.Length; i++)
{
Console.WriteLine(types[i].FullName);
}
//StudyLib.MyClass //动态创建类并调用其中的方法
Type t = assembly.GetType("StudyLib.MyClass");
ConstructorInfo constructor = t.GetConstructor(Type.EmptyTypes);
Object myClass = constructor.Invoke(new object[]{}); MethodInfo method1 = t.GetMethod("Add", new[] {typeof (int), typeof (int)});
int result1 = (int)method1.Invoke(myClass, new object[] {, });
Console.WriteLine("第一个方法返回: " + result1);
//第一个方法返回: 223 MethodInfo method2 = t.GetMethod("Add", new[] { typeof(int), typeof(int), typeof(int)});
int result2 = (int)method2.Invoke(myClass, new object[] { , , });
Console.WriteLine("第二个方法返回: " + result2);
//第二个方法返回: 679 Console.Read();
}
}
}
C#学习笔记(十):反射的更多相关文章
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- Learning ROS for Robotics Programming Second Edition学习笔记(十) indigo Gazebo rviz slam navigation
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 moveit是书的最后一章,由于对机械臂完全不知,看不懂 ...
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...
- python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法
python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法 同一台机器同时安装 python2.7 和 python3.4不会冲突.安装在不同目录,然 ...
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
- python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
python3.4学习笔记(十五) 字符串操作(string替换.删除.截取.复制.连接.比较.查找.包含.大小写转换.分割等) python print 不换行(在后面加上,end=''),prin ...
- python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL
python3.4学习笔记(十二) python正则表达式的使用,使用pyspider匹配输出带.html结尾的URL实战例子:使用pyspider匹配输出带.html结尾的URL:@config(a ...
- python3.4学习笔记(十) 常用操作符,条件分支和循环实例
python3.4学习笔记(十) 常用操作符,条件分支和循环实例 #Pyhon常用操作符 c = d = 10 d /= 8 #3.x真正的除法 print(d) #1.25 c //= 8 #用两个 ...
- Go语言学习笔记十二: 范围(Range)
Go语言学习笔记十二: 范围(Range) rang这个关键字主要用来遍历数组,切片,通道或Map.在数组和切片中返回索引值,在Map中返回key. 这个特别像python的方式.不过写法上比较怪异使 ...
- Go语言学习笔记十: 结构体
Go语言学习笔记十: 结构体 Go语言的结构体语法和C语言类似.而结构体这个概念就类似高级语言Java中的类. 结构体定义 结构体有两个关键字type和struct,中间夹着一个结构体名称.大括号里面 ...
随机推荐
- bzoj1876: [SDOI2009]SuperGCD
更相减损数. 上手就debug了3个小时,直接给我看哭了. 3个函数都写错了是什么感受? 乘2函数要从前往后乘,这样后面的数乘2进位以后不会干扰前面的数. 除2函数要从后往前除,这样前面的数借来的位不 ...
- iOS开发:视图生命周期
iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...
- UVa 1149 Bin Packing 【贪心】
题意:给定n个物品的重量l[i],背包的容量为w,同时要求每个背包最多装两个物品,求至少要多少个背包才能装下所有的物品 和之前做的独木舟上的旅行一样,注意一下格式就好了 #include<ios ...
- tomcat 默认项目设置
正常情况下,我们启动tomcat后,直接输入“http://localhost:端口/“ 后,默认访问道是webapp目录下的ROOT应用. 我们要通过上述方式访问自己的应用,有俩种方式. 第一:把自 ...
- uestc 1721 吴神,人类的希望
// 将n个相同的球放进m个盒子 盒子不为空的方法总数// dp[i][j] 表示i个盒子 j个球的方法总数// 递推关系 dp[i][j]=dp[i-1][j-1]+d[i][j-i]// a. i ...
- 【轻院热身赛】级数求和、进制转换、candy
[题目链接:级数求和] Problem A: 级数求和 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 409 Solved: 240 SubmitSt ...
- 【转】Linux Posix Timer使用
原文网址:http://blog.csdn.net/hongszh/article/details/8608781 最强大的定时器接口来自POSIX时钟系列,其创建.初始化以及删除一个定时器的行动被分 ...
- CMake实践(1)
简介: 目录结构t1/main.cpp; t1/CMakeLists.txt 说明: main.cpp: #include <stdio.h> int main(){ printf( ...
- C# 一次查询多表,填充DataSet并指定表名
lhrhi 原文 NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧) 现实中的场景,有时可能需要一次查询数据库中表张.在使用SqlDataAdapte ...
- Maximum Product Subarray JAVA实现
题目描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...