Introduction
Let’s say we have devices of different model with the same functionality. How to use them in a single project? In this article, we will see creating class libraries for each of these device models and using them in one project with the aid of Reflection technology.
Using the Code
First of all, we create class library project named “DeviceCommon
” and we add new interface which includes logic of all devices.
IDevice.cs
public interface IDevice
{
bool Open();
bool Reset();
DeviceStatus GetStatus();
void Initialize();
string GetModel();
}
Now, we create class library project for each of the device models. Each class library contains different logic and Implementation of IDevice
interface which will be invoked from the former project. Below is a suitable code fragment:
This code fragment is for device of model named “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();
}
}
This code fragment is for device of model named “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();
}
}
Other device library projects also can be created in this way, although the logic they contain will differ.
Now we create main project to use these devices. By reflection, it’s enough to add “DeviceCommon
” to project reference in order to use class libraries, because all device models extended from IDevice
interface in “DeviceCommon
” class library project. This is the power of reflection: Using Interface, we can find all class objects extended from it and we can get access to all their accessible members and methods. For facility, we will create App.config configuration file and add DLL reference to it. It provides the opportunity to re-build the project automatically after release.
App.config
="1.0"="utf-8"
<configuration>
<appSettings>
<add key="DllFileName" value="Device1.dll"/>
</appSettings>
</configuration>
Code fragment to use in the main project. Included here is code to invoke each class libraries shown in App.config dynamically using reflection:
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();
}
}
History
- 24th June, 2019: Initial version