C#知识点-反射
一、开发环境
操作系统:Win7
编译器:VS2010
.net版本:.net4.0
二、项目结构

三、开发流程
0.编写实体类
namespace ReflectDemo
{
public class Bird
{
public string _id; public string Name { get; set; } public int Age { get; set; } public void Eat()
{
Console.WriteLine("我是个吃货");
} public void Eat(string birdName)
{
Console.WriteLine("我和" + birdName + "都是个吃货");
} public Bird()
{ } public Bird(string name, int age)
{
this.Name = name;
this.Age = age;
} public void BirdIntroducion()
{
Console.WriteLine("我叫" + Name + ",我" + Age + "岁了");
}
}
}
1.获取Assembly对象
namespace ReflectDemo
{
public class GetAssembly
{
public void MethodGetAllAssembly()
{
//获取当前应用程序域中的Assembly
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Console.WriteLine(assemblies.ToString());
} public void MethodGetCurrentObjAssembly()
{
//获取当前对象所在的Assembly
Assembly assembly = this.GetType().Assembly;
Console.WriteLine(assembly.ToString());
} public void MethodGetFromBin()
{
//获取bin目录下的指定Assembly
Assembly assembly = Assembly.LoadFrom("ReflectDemo.exe");
Console.WriteLine(assembly.ToString());
}
}
}
2.获取Type对象
namespace ReflectDemo
{
public class GetType
{
public void MethodGetByClassName()
{
//通过类名获得Type
Type type = typeof(Bird);
Console.WriteLine(type.ToString());
} public void MethodGetByObjName()
{
//通过对象名获得Type
Bird bird = new Bird();
Type type = bird.GetType();
Console.WriteLine(type.ToString());
} public void MethodGetByFullName()
{
//通过 命名空间.类名获取
Assembly assembly = this.GetType().Assembly;
Type type = assembly.GetType("ReflectDemo.Bird");
} public void MethodGetAll()
{
//获取Assembly中所有的类型
Assembly assembly = this.GetType().Assembly;
Type[] types = assembly.GetTypes();
} public void MethodGetAllPublic()
{
//获取Assembly中定义的所有public类
Assembly assembly = this.GetType().Assembly;
Type[] types = assembly.GetExportedTypes();
}
}
}
3.获取Type成员对象
3.1获取字段信息
namespace ReflectDemo
{
public class GetFieldInfo
{
public void MethodGetPublicField()
{
Bird bird = new Bird()
{
_id = "1"
};
Type type = bird.GetType();
FieldInfo idInfo = type.GetField("_id");
string id = idInfo.GetValue(bird).ToString();
Console.WriteLine(id);
idInfo.SetValue(bird, "2");
string newId = bird._id;
Console.WriteLine(newId);
}
}
}
3.2获取属性信息
namespace ReflectDemo
{
public class GetPropertyInfo
{
public void MethodGetAllPublic()
{
Bird bird = new Bird()
{
Name = "小黄"
};
Type type = bird.GetType();
PropertyInfo nameInfo = type.GetProperty("Name");
string name = nameInfo.GetValue(bird, null).ToString();
Console.WriteLine(name);
nameInfo.SetValue(bird, "小黄黄", null);
Console.WriteLine(bird.Name);
}
}
}
3.3获取方法信息
namespace ReflectDemo
{
public class GetMethodInfo
{
public void MethodGetWithNoParas()
{
Bird bird = new Bird();
Type type = bird.GetType();
MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { });
eatMethodInfo.Invoke(bird, null);
} public void MethodWithParas()
{
Bird bird = new Bird();
Type type = bird.GetType();
MethodInfo eatMethodInfo = type.GetMethod("Eat", new Type[] { typeof(string) });
eatMethodInfo.Invoke(bird, new object[] { "小黑" });
}
}
}
3.4获取构造函数
namespace ReflectDemo
{
public class GetConstructorInfo
{
public void MethodGetActivator()
{
Type type = typeof(Bird);
Bird bird = Activator.CreateInstance(type, new object[] { "小白", 3 }) as Bird;
bird.BirdIntroducion();
} public void MethodGetConstructor()
{
Type type = typeof(Bird);
ConstructorInfo ctor = type.GetConstructor(new Type[] { typeof(string), typeof(int) });
Bird bird = ctor.Invoke(new object[] { "小黑", 5 }) as Bird;
bird.BirdIntroducion();
}
}
}
4.编写控制台程序
namespace ReflectDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---获取Assembly---");
GetAssembly getAssembly = new GetAssembly();
getAssembly.MethodGetAllAssembly();
getAssembly.MethodGetCurrentObjAssembly();
getAssembly.MethodGetFromBin(); Console.WriteLine("\n---获取Type对象---");
GetType getType = new GetType();
getType.MethodGetByClassName();
getType.MethodGetByObjName();
getType.MethodGetByFullName();
getType.MethodGetAll();
getType.MethodGetAllPublic(); Console.WriteLine("\n---获取字段信息---");
GetFieldInfo getFieldInfo = new GetFieldInfo();
getFieldInfo.MethodGetPublicField(); Console.WriteLine("\n---获取属性信息---");
GetPropertyInfo getPropertyInfo = new GetPropertyInfo();
getPropertyInfo.MethodGetAllPublic(); Console.WriteLine("\n---获取方法信息---");
GetMethodInfo getMethodInfo = new GetMethodInfo();
getMethodInfo.MethodGetWithNoParas();
getMethodInfo.MethodWithParas(); Console.WriteLine("\n---获取构造函数信息---");
GetConstructorInfo getConstructorInfo = new GetConstructorInfo();
getConstructorInfo.MethodGetActivator();
getConstructorInfo.MethodGetConstructor(); Console.ReadKey();
}
}
}
四、项目说明
1.什么是反射:
(1).在程序运行时,
动态 获取 加载程序集
动态 获取 类型(类,接口)
动态 获取 类型的成员 信息(字段,属性,方法)
(2).在运行时,
动态 创建类型实例,以及 调用 和访问 这些 实例 成员
程序集(Assembly对象)===》类,接口(Type对象)===》类的成员(**Info)
五、其他信息
C#知识点-反射的更多相关文章
- 6.C#知识点:反射
1.反射是什么? 反射提供描述组件,模块和类型的对象(类型为Type).您可以使用反射来动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型,并调用其方法或访问其字段和属性.如果您在代码中使 ...
- 跟着刚哥梳理java知识点——反射和代理(十七)
反射机制是什么?反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有的属性和方法:对于任意一个对象,都能够调用他的一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...
- Java基础重要知识点-反射
反射,如何把.java文件转化为.class文件 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信 ...
- 小白也能看懂的插件化DroidPlugin原理(二)-- 反射机制和Hook入门
前言:在上一篇博文<小白也能看懂的插件化DroidPlugin原理(一)-- 动态代理>中详细介绍了 DroidPlugin 原理中涉及到的动态代理模式,看完上篇博文后你就会发现原来动态代 ...
- 反射的妙用:C#通过反射动态生成类型继承接口并实现
起因 最近想自己鼓捣个RPC,想着简化RPC调用方式,直接申明接口,然后根据接口的属性去配置RPC调用的相关信息.有一种说法叫申明式调用. 简单来说就是,申明一个interface,动态继承并实例化, ...
- Java程序员都要懂得知识点:反射
摘要:Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...
- Java基础--反射机制的知识点梳理
什么是反射? 正常编译执行java文件时,会生成一个.class文件,反射就是一个反编译的过程,它可以通过.class文件得到一个java对象.一个类会有很多组成部分,比如成员变量,成员方法,构造方法 ...
- java 反射,注解,泛型,内省(高级知识点)
Java反射 1.Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs 取得任何一个已知名称的class的内部信息, 包括 ...
- JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]
类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...
随机推荐
- Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LENOVO System x3850 X6
http://appserver.lenovo.com.cn/Lenovo_Series_List.aspx?CategoryCode=A31B01
- 【CV论文阅读】生成式对抗网络GAN
生成式对抗网络GAN 1. 基本GAN 在论文<Generative Adversarial Nets>提出的GAN是最原始的框架,可以看成极大极小博弈的过程,因此称为“对抗网络”.一般 ...
- Java实现二叉搜索树及相关操作
package com.tree; import com.tree.BitNode; /** * * 二叉搜索树:一个节点的左子节点的关键字小于这个节点.右子节点的关键字大于或等于这个父节点 * * ...
- 第二十七篇:Windows驱动中的PCI, DMA, ISR, DPC, ScatterGater, MapRegsiter, CommonBuffer, ConfigSpace
近期有些人问我PCI设备驱动的问题, 和他们交流过后, 我建议他们先看一看<<The Windows NT Device Driver Book>>这本书, 个人感觉, 这本书 ...
- 编程算法 - 最小的k个数 代码(C)
最小的k个数 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入n个整数, 找出当中的最小k个数. 使用高速排序(Quick Sort)的方法 ...
- 菜鸟的mongoDB学习---(五)MongoDB的limit、skip、sort方法
limit方法 假设你须要在MongoDB中读取指定数量的数据记录.能够使用MongoDB的Limit方法,limit()方法接受一个数字參数,该參数指定从MongoDB中读取的记录条数. mongo ...
- HDU 1018 Big Number (log函数求数的位数)
Problem Description In many applications very large integers numbers are required. Some of these app ...
- [译]IOS中AutoLayout布局与Transform的冲突问题
http://m.blog.csdn.net/blog/a345017062/43565279 原文链接见这里: http://stackoverflow.com/questions/12943107 ...
- python 变量作用域 v.__sizeof__() python 深复制 一切皆对象 尽量减少内存消耗
python 深入理解 赋值.引用.拷贝.作用域 - 江召伟 - 博客园 https://www.cnblogs.com/jiangzhaowei/p/5740913.html a=[1,2,5]b= ...