您当前的位置: 首页 > 

寒冰屋

暂无认证

  • 0浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

在C#中使用反射的简单例子

寒冰屋 发布时间:2019-06-27 09:31:54 ,浏览量:0

目录

介绍

使用代码

  • 下载示例 - 14.1 KB
介绍

假设我们拥有具有相同功能的不同型号的设备。如何在一个项目中使用它们?在本文中,我们将看到为每个设备模型创建类库,并在反射技术的帮助下在一个项目中使用它们。

使用代码

首先,我们创建名为“DeviceCommon”的类库项目,并添加包含所有设备逻辑的新接口。

IDevice.cs

/// 
///
/// 
public interface IDevice
{
    /// 
    ///
    /// 
    /// 
    bool Open();

    /// 
    ///
    /// 
    /// 
    bool Reset();

    /// 
    ///
    /// 
    /// 
    DeviceStatus GetStatus();
    void Initialize();

    /// 
    ///
    /// 
    /// 
    string GetModel();
}

现在,我们为每个设备模型创建类库项目。每个类库包含不同的逻辑和IDevice接口的实现,它将从前一个项目调用。下面是一个合适的代码片段:

此代码片段用于名为“Device1” 的模型的设备。

Device1.cs

public class Device1 : IDevice
    {
        public string GetModel()
        {
            return "Device1";
        }

        public DeviceStatus GetStatus()
        {
            throw new System.NotImplementedException();
        }

        public void Initialize()
        {
            throw new System.NotImplementedException();
        }

        public bool Open()
        {
            throw new System.NotImplementedException();
        }

        public bool Reset()
        {
            throw new System.NotImplementedException();
        }
    }

此代码片段用于名为“Device2” 的模型的设备。

Device2.cs

public class Device2 : IDevice
    {
        public string GetModel()
        {
            return "Device2";
        }

        public DeviceStatus GetStatus()
        {
            throw new System.NotImplementedException();
        }

        public void Initialize()
        {
            throw new System.NotImplementedException();
        }

        public bool Open()
        {
            throw new System.NotImplementedException();
        }

        public bool Reset()
        {
            throw new System.NotImplementedException();
        }
    }

其他设备库项目也可以这种方式创建,尽管它们包含的逻辑会有所不同。

现在我们创建使用这些设备的主项目。通过反射,只需在项目引用中添加“DeviceCommon”就可以使用类库,因为所有设备模型都是从“DeviceCommon”类库项目中的IDevice接口扩展而来的。这是反射的力量:使用接口,我们可以找到从它扩展的所有类对象,并且我们可以访问所有可访问的成员和方法。对于工具,我们将创建App.config配置文件并向其添加DLL引用。它提供了在发布后自动重新构建项目的机会。

App.config



  
    
  

要在主项目中使用的代码片段。这里包含使用反射动态调用App.config中显示的每个类库的代码:

Program.cs

class Program
    {
        static void Main(string[] args)
        {
            string path = Environment.CurrentDirectory + @"\";
            var dllFileName = ConfigurationSettings.AppSettings["DllFileName"];

            if (dllFileName == null)
                throw new Exception("Dll file not shown in App.config!");

            string fileForLoad = path + dllFileName;
            if (!File.Exists(fileForLoad))
                throw new Exception("Dll file not found!");

            var myAssembly = Assembly.LoadFile(fileForLoad);
            var serviceType = typeof(IDevice);
            var types = myAssembly.GetTypes().ToList();
            var myServiceType = types.Where(w => 
            serviceType.IsAssignableFrom(w)).ToList().FirstOrDefault();
            if (myServiceType == null)
                throw new Exception("Service not found!");

            var myDevice = Activator.CreateInstance(myServiceType) as IDevice;
            Console.WriteLine("Device model: "+myDevice.GetModel());
            Console.ReadKey();
        }
    }

 

原文地址:https://www.codeproject.com/Tips/5160372/Using-Reflections-in-Csharp

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0519s