开发者

.NET 3.5: Dynamically adding classes without recompiling

I'd like to build an infrastructure that will monitor a server and check ping, response time, and 开发者_如何学Cmore.

The catch is that future checks (for example: does a certain file exist) will be added without recompiling the infrastructure.

How do I build it in such a way that will enable me to attach inherited classes with different functionality, and execute them without recompiling?

Thanks!


In addition to creating an interface and defining a single entry point for your new library, you could create an attribute which identifies the classes that you need to load or the methods that you need to call. You then use reflection to look at all the DLLs in a certain path, and instantiate / run whatever contains your attribute.

I've built a similar application that had to perform a number of health checks on a system, and needed to be extensible. The application started, looked through all the DLLs in a specified path and for each class with the 'TestAttribute' decoration it would create an instance and run the 'Execute' method.

The use of an attribute means that you don't need to specify which DLLs to process (doesn't need to be in config / database) because it's safe to process every DLL, and only those decorated with the attribute will do anything.


Implement an interface, and the provider pattern, then you can plug anything in that you like. MSBuild is a great example of this, with a simple interface you can add any type of task you like to your build process - follow the same sort of pattern.


Sounds like you could use some kind of 'plugin' mechanism. Define a basic interface and you can compile every "check/action" into a separate assembly. Load all your assemblies dynamically from file and call execute the check/action via the defined interface.

The interface could be just as simple as this, for starters:

public interface IMonitorAction
{
    bool Exectute();
}

This infrastructure allows you to add more checks by just creating another assembly file implementing the interface next to the existing ones.


Of the top of my head.

I presume you can re-start you application. Have a file that lists all the DLL's to load that implement your required functionality. Each DLL should have the same name entry point. Load each DLL, call the method, unload DLL. loop.

Caveat: I've never done anything like this, so I may be talking hot air.


Adding to @slugsters answer, instead of building your own extensibility infrastructure, take a look at extensibility libraries like MEF.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜