views:

55

answers:

2

How can I store C# methods in a sql server instance to be loaded and run dynamically (at runtime) at a later date?

I am about to begin a production test project at work where I would like the appropriate test methods for the product to be loaded at runtime.

I have looked at Reflection.Emit DynamicMethod briefly but it is not serializable.

Any suggestions or alternative routes would be appreciated.

A: 

Serialising classes does not serliase any code it just serialises data and the reloads that data into the properties of the class when it is deserialised. Any methods on the class are just the same as if you had just instantiated the class manually. Trying to serialise actual code to a database and then somehow compile it into a method from at run time is a pretty whacky and exotic thing to do and i am strugling to understand how it would be useful in the context of testing. If you really really want to store the source code in the database you would be better off storing it as text, retreiving it into a text file then programtically compiling it. However this is what source control and build systems are for such as Subversion and Nant, so you would arguably be reinventing the wheel.

Further to your last comment i think a good pattern for what you require would be dependency injection. You create an interface (e.g. ITester) that defines the contract for a test, a simple one would be a test method that returns a bool for pass and fail. You write your testing program as normal, but your test method takes a class that implements the ITest interface. The class that implements the ITester interface is reponsible for the specific test implementation and actually carries out the test. THe rest of the system does not care what the test is or how it is carried out, it simply runs a test method that returns true or false.

That way all you have to do to create a new test implementation is create a new class that implements ITester. If you need to change a test implementation you just alter the particular class and redeploy. If you compile each tester class into it's own DLL, you can store these as binary data in the database. You can then extract them from the database and load them dynamically using System.Reflection.Assembly.Load. Note, this is not serialisation, you are storing the binary assembly file in the datbase.

What you end up with is a system that decides what test it needs to run, queries the database for the relevent DLL file that can run the test code, then dynamically loads and executes the DLL at run time.

Ben Robinson
Thanks for your answer Ben.I understand that the source code would not be stored in the database when serializing an object but in looking around for a method of performing the task I came across the dynamic method class (I'm fairly new in professional software development) and thought that the DynamicMethod (being an object itself) may somehow be stored in a serializable manner.
Lewray
My point was not so much that source code would not be stored in the database when serialising an object, more that NO code at all will be stored. Serlialising simply stored the data and relies on an actual compiled class to deserialise back to. Having said that I can see what you mean with DynamicMethod, as the data is the code so to speak. Even if you coudl serlialise it i am nto sure how useful it would be. What are you actually trying to acheive as your description sounds liek a bizare thing to want to do.
Ben Robinson
That was my thinking - the code is the data in a DynamicMethod.The future scenario is (hopefully) hundreds of different electronic products which need a number of tests to be run on them specific to the hardware components used to build them. An operator enters the product part number into the Product Test application and all tests relating to the specific hardware configuration on that product are loaded and run.
Lewray
That sounds like a nice implementation and storing the assembly in the database would provide the dynamic loading of methods I was after.I will have to determine the approximate size of the dll files and determine how scalable it is as a solution, but it certainly gives me a good starting point.Thanks (if I had enough rep I would +1)
Lewray
Compile the interface (ITester) into e separate dll that you don't recompile to make sure you don't break existing plugins when releasing future versions of your main application.
Albin Sunnanbo
A: 

Out of interest, if you use Expression<> trees for your methods, there is a project on the go to serialize these - see the SO post here

nonnb