C# 反射

反射:
assembly: 程序集
Module 模块
class 类型

提供一种编程方式 让程序在运行期间获得这几个组成部分的相关信息

好处:
反射可以让你的程序在更新的时候无需重新编译,只需替换新的dll;
可以开发出来随意增删该功能的软件 提高系统的灵活性和扩展性
可以用来修改封装好的类中的东西

缺点:
反射是一种解释操作,性能慢
发射使程序内部逻辑模糊

例子:
namespace com.bblong.lp
{
public class Student
{
public string Name{set;get;}
pulbic int Age{set;get;}

public Student()
{
this.Age=24;
this.Name="zhangsan";
}


public void Hello()
{
Console.WriteLine("我是"+Name+",年龄:"+Age);


}
}

}


namespace com.bblong.lp
{
public Demo1
{
// 动态加载dll 
var asm = Assembly.LoadFile(@"C:\User\......\Com.bblong.lp.Student.dll");
// 获取Student 类型
var type = asm.GetType("Com.bblong.lp.Student");


// 创建类型的实例
var instance = asm.CreateInstance("Com.bblong.lp.Student");


// 为实例的属性赋值
type.GetProperty("Name").SetValue(instance,"bblonglp",null);
type.GetProperty("Age").SetValue(instance,26,null);


// 获取实例的方法
var method = type.GetMethod("Hello");
// 调用实例的方法
method.Invoke(instance,null);  
Console.Read();
}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值