c#反射技术

反射介绍

简介

     反射:反射是一个普通的术语,它描述了在运行过程中检查和处理程序元素的功能。也就是在程序运行时可以控制程序,不是在编译的时候。开始学习反射就学董两个类就可以。

Type类

   通过这个类可以访问关于任何数据类型的信息。Type类只为存储类型的引用,是许多反射功能的入口。

实例化对象

方法一得到数据类型
<span style="font-size:18px;">           Type t1=typeof(int);

            Type t2=typeof(double);</span>
方法二从变量中得到

<span style="font-size:18px;">      int n = 1;

            double d = 0.1;

            Type t3 = n.GetType();

            Type t4 = d.GetType();</span>


对象的属性和方法

属性

常用的属性也就是判断数据的类型返回bool类型

IsClass,IsArray,IsEnum,IsValueType

方法

得到类型的所有方法:

<span style="font-size:18px;">  MethodInfo[] methods =t1.GetMethods();

            foreach (MethodInfo method in methods)

            {

                Console.WriteLine(method.Name);

            }</span>


同样还可以得到类的所有属性 字段 事件

Assembly

  类允许访问程序集的元数据,它包含可以加载和执行程序集的方法。动态得到类型从dll库中获取

新建类库

<span style="font-size:18px;">namespace Test

{

    classPerson

    {

        privatestring _name;

 

        publicstring Name

        {

            get { return _name; }

            set { _name = value; }

        }

        privatestring _address;

 

        publicstring Address

        {

            get { return _address; }

            set { _address = value; }

        }

        privateint _age;

 

        publicint Age

        {

            get { return _age; }

            set { _age = value; }

        }

        public Person(string name, string address, int age)

        {

            this._name = name;

            this.Address = address;

            this._age = age;

        }

        publicstring SayHi()

        {

            return"Hello ,My nameis" + this._name;

        }

    }

}</span>


将生成的dll类库存放的一个特定位置,

创建类型

<span style="font-size:18px;">Assembly asse = Assembly.LoadFile(@"F:\C++\Demo\RefelectDemo\bin\Debug\Person.dll");

            Type t5 = asse.GetType("Test.Person");

            Console.WriteLine("Person 类的方法");

            MethodInfo[] methods2 =t5.GetMethods();

            foreach (MethodInfo m in methods2)

            {

                Console.WriteLine(m.Name);

            }</span>


动态传建实体

根基动态得到的type类型创建对象

<span style="font-size:18px;">  DateTime dt = (DateTime)Activator.CreateInstance(typeof(DateTime));</span>


总结

动态控制给编程者带来了很多方便。一项具体应用实例

http://www.csharp-examples.net/reflection-examples/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#反射是一种强大的编程技术,它允许我们在运行时检查程序集、模块和类型,并动态地创建实例、调用方法、访问字段和属性等。下面是一个示例代码,展示了如何使用C#反射来加载和使用DLL。 ```csharp using System; using System.Reflection; namespace ReflectionExample { public interface IPlugin { void Run(); } public class PluginA : IPlugin { public void Run() { Console.WriteLine("PluginA is running."); } } public class PluginB : IPlugin { public void Run() { Console.WriteLine("PluginB is running."); } } class Program { static void Main(string[] args) { // 动态加载DLL并获取类型 string dllPath = "path/to/your/plugin.dll"; Assembly assembly = Assembly.LoadFile(dllPath); Type pluginType = assembly.GetType("Namespace.PluginA"); // 创建实例并调用方法 IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType); plugin.Run(); } } } ``` 上述代码中,我们定义了一个接口`IPlugin`,并在DLL中实现了两个类`PluginA`和`PluginB`来实现该接口。在`Main`方法中,我们使用`Assembly.LoadFile`方法动态加载了DLL,并使用`assembly.GetType`方法获取了`PluginA`的类型。然后,我们使用`Activator.CreateInstance`方法创建了`PluginA`的实例,并通过调用其`Run`方法来运行插件的功能。 需要注意的是,上述示例仅仅展示了C#反射的基本用法,实际应用中可能会更加复杂。在实际使用中,你需要替换`"path/to/your/plugin.dll"`为你实际的DLL路径,以及`"Namespace.PluginA"`为你实际的类型名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值