一、开发环境

操作系统: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#知识点-反射的更多相关文章

  1. 6.C#知识点:反射

    1.反射是什么? 反射提供描述组件,模块和类型的对象(类型为Type).您可以使用反射来动态创建类型的实例,将类型绑定到现有对象,或从现有对象获取类型,并调用其方法或访问其字段和属性.如果您在代码中使 ...

  2. 跟着刚哥梳理java知识点——反射和代理(十七)

    反射机制是什么?反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有的属性和方法:对于任意一个对象,都能够调用他的一个方法和属性,这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...

  3. Java基础重要知识点-反射

    反射,如何把.java文件转化为.class文件 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信 ...

  4. 小白也能看懂的插件化DroidPlugin原理(二)-- 反射机制和Hook入门

    前言:在上一篇博文<小白也能看懂的插件化DroidPlugin原理(一)-- 动态代理>中详细介绍了 DroidPlugin 原理中涉及到的动态代理模式,看完上篇博文后你就会发现原来动态代 ...

  5. 反射的妙用:C#通过反射动态生成类型继承接口并实现

    起因 最近想自己鼓捣个RPC,想着简化RPC调用方式,直接申明接口,然后根据接口的属性去配置RPC调用的相关信息.有一种说法叫申明式调用. 简单来说就是,申明一个interface,动态继承并实例化, ...

  6. Java程序员都要懂得知识点:反射

    摘要:Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语 ...

  7. Java基础--反射机制的知识点梳理

    什么是反射? 正常编译执行java文件时,会生成一个.class文件,反射就是一个反编译的过程,它可以通过.class文件得到一个java对象.一个类会有很多组成部分,比如成员变量,成员方法,构造方法 ...

  8. java 反射,注解,泛型,内省(高级知识点)

     Java反射 1.Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs    取得任何一个已知名称的class的内部信息, 包括 ...

  9. JAVA常用基础知识点[继承,抽象,接口,静态,枚举,反射,泛型,多线程...]

    类的继承 Java只支持单继承,不允许多重继承- 一个子类只能有一个父类- 一个父类可以派生出多个子类这里写图片描述子类继承了父类,就继承了父类的方法和属性.在子类中,可以使用父类中定义的方法和属性, ...

随机推荐

  1. 洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are h ...

  2. 安卓常见错误Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.

    Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace. 导入新的 ...

  3. Ubuntu 16.04出现chmod: 无效模式:"a"的问题解决

    命令: chmod a+x file1 提示:注意文件的类型,如果用在文件夹上是不行的,但是文件确实可以的.

  4. 创建Filter步骤

    创建Filter步骤: 创建Filter处理类 必须实现javax.servlet.Filter,该接口有init()完成filter初始化,destroy()完成资源回收,doFilter()过滤 ...

  5. anaconda是个啥?

    Anaconda具有跨平台(win/mac/linux).包管理(类似于pip).环境管理(类似于virtualenv)的特点,因此很适合快速在新的机器上部署Python环境 Anaconda利用工具 ...

  6. 条款十: 如果写了operator new就要同时写operator delete

    为什么有必要写自己的operator new和operator delete? 答案通常是:为了效率.缺省的operator new和operator delete具有非常好的通用性,它的这种灵活性也 ...

  7. 经常使用的android设计模式

    一般来说,经常使用的android设计模式有下面8种:单例.工厂.观察者.代理.命令.适配器.合成.訪问者.   单例模式:目的是为了让系统中仅仅有一个调用对象,缺点是单例使其它程序过分依赖它,并且不 ...

  8. Cannot change version of project facet Dynamic Web Module to 3.1 (Eclipse Maven唯一解决方式)

    If you want to use version 3.1 you need to use the following schema: http://xmlns.jcp.org/xml/ns/jav ...

  9. A program to print Fahrenheit-Celsius table with floating-point values

    我的主力博客:半亩方塘 Another program to print Fahrenheit-Celsius table with decimal integer This program is p ...

  10. Oracle中长度为0字符串与null等价

    不试不知道,Oracle中,长度为0的字符串'' 居然与null等价! 众所周知,null代表空,什么都不存在,而一个字符串'',虽然长度为0,但毕竟已经是一个字符串,二者怎么能等价,混为一谈呢. 在 ...