反射的简介
Reflection,中文翻译为反射。
这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’、‘模块(Module)’、‘类型(class)’组成,而反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组成部分的相关信息,例如:
Assembly类可以获得正在运行的装配件信息,也可以动态的加载装配件,以及在装配件中查找类型信息,并创建该类型的实例。
Type类可以获得对象的类型信息,此信息包含对象的所有要素:方法、构造器、属性等等,通过Type类可以得到这些要素的信息,并且调用之。
MethodInfo包含方法的信息,通过这个类可以得到方法的名称、参数、返回值等,并且可以调用之。
诸如此类,还有FieldInfo、EventInfo等等,这些类都包含在System.Reflection命名空间下。
反射指程序可以访问、检测和修改它本身状态或行为的一种能力。
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。
反射的简单使用
您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
参考代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace 反射1
{
class Student
{
private string name;
private int age;
private char gender;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public char Gender
{
get
{
return gender;
}
set
{
gender = value;
}
}
public Student()
{
}
public Student(string name, int age, char gender)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
}
public void ShowInfoMation()
{
Console.WriteLine("姓名:{0}年龄:{1}性别:{2}",Name,Age,Gender);
}
}
class Program
{
static void Main(string[] args)
{
Type type = typeof(int);
Console.WriteLine(type.Assembly);
Console.WriteLine(type.IsAbstract);
Console.WriteLine(type.IsClass);
Console.WriteLine(type.IsArray);
Console.WriteLine(type.Namespace);
Console.WriteLine(type.Name);
Console.WriteLine(type.FullName);
Console.WriteLine(type.BaseType);
Console.WriteLine("-----------反射的开始-----------");
Type t = Type.GetType("反射1.Student");
Console.WriteLine(t.Name);
Console.WriteLine("-------------------");
Console.WriteLine(t.BaseType);
Assembly assem = Assembly.GetAssembly(t);
Console.WriteLine(assem.FullName);
Console.WriteLine(assem.EntryPoint);
object studentObj = assem.CreateInstance("反射1.Student");
Type studentType = studentObj.GetType();
FieldInfo[] fieldInfoArr = studentType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
Console.WriteLine(fieldInfoArr.Length +"++");
foreach (var item in fieldInfoArr)
{
Console.WriteLine(item);
}
PropertyInfo[] propInfoArr = studentType.GetProperties();
foreach (var item in propInfoArr)
{
Console.WriteLine(item);
}
MethodInfo[] methodinfoArr = studentType.GetMethods();
foreach (var item in methodinfoArr)
{
Console.WriteLine(item);
}
PropertyInfo propName = studentType.GetProperty("Name");
propName.SetValue(studentObj,"Jack");
PropertyInfo propAge = studentType.GetProperty("Age");
propAge.SetValue(studentObj,20);
PropertyInfo propGender = studentType.GetProperty("Gender");
propGender.SetValue(studentObj, '男');
MethodInfo methodShow = studentType.GetMethod("ShowInfoMation");
methodShow.Invoke( studentObj,null);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
使用反射配置文件
在工程目录下(如图)面添加的一个类库和一个配置文件
类库中的代码如下
实现一个接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Input
{
interface IMyInput
{
int Caculate(int num1, int num2);
}
}
三个继承接口的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Input
{
class TomInput : IMyInput
{
public int Caculate(int num1, int num2)
{
return num1 + num2;
}
}
class MaryInput : IMyInput
{
public int Caculate(int num1, int num2)
{
return num1 - num2;
}
}
class JackInput : IMyInput
{
public int Caculate(int num1, int num2)
{
return num1 * num2;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
相关的逻辑代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
namespace 通过反射配置文件
{
class Program
{
static void Main(string[] args)
{
string configPath = Path.GetFullPath(@"Config.txt");
StreamReader sr = new StreamReader(configPath,Encoding.Default);
List<string> list = new List<string>();
string str = string.Empty;
while( (str =sr.ReadLine())!= null)
{
list.Add(str);
}
string ret = string.Empty;
foreach (var item in list)
{
if (!item.StartsWith("#"))
{
ret = item;
}
}
Console.WriteLine(ret);
Type t = Type.GetType(ret);
string path = Path.GetFullPath(@"Input.dll");
Assembly assem = Assembly.LoadFile(path);
Type[] typeArr = assem.GetTypes();
foreach (var item in typeArr)
{
Console.WriteLine(item);
}
object obj = assem.CreateInstance(ret);
Type objType = obj.GetType();
MethodInfo[] methodInfoArr = objType.GetMethods();
foreach (var item in methodInfoArr)
{
Console.WriteLine(item);
}
MethodInfo method = objType.GetMethod("Caculate");
object result = method.Invoke(obj, new object[] { 6, 2 });
Console.WriteLine(result);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
我们在使用的时候只需要在Config文件中添加或者删除”#”号就可以达到动态使用程序的效果
【注意】确保里面只有一行不加”#”号