This topic is now archived and is closed to further replies.


Design requirments for a code cache in C#

Recommended Posts

I''ve been working on a few hobby projects which require the emitting of custom MSIL. I have realised that using reflection to search for some types, generating custom classes + MSIL and then generating usable types & assembles is fairly slow. Especially if the exact same setups with the same results are done everytime the application is loaded. So I thought that caching the generated code to disk to be persistant might help. I know I need to take steps to detect if the contents of the cache has gone stale, and this will involve creating a list of hashes for the sourcefiles(assemblies), but havent really worked much on the implementation details. I can futhur reduce the requirments of using reflection on the cached assembly by hardcoding a class/function into the generated DLLs which return various pre-compiled data about the saved types. The main project which started this idea is trying to implement a runtime creation of factory classes for an abstract factory. Most of the time, all you are doing is adding a different hardcoded call to some object''s constructor with some arbitary parameters. So say you have the following abstract factory type and the base class it is to create:
	public abstract class DemoMetaClass
	#region Properties
	/// <summary>

	/// Returns the classtype of the class wrapped by the meta/factory class. 

	/// If a abstract property, then the result is hardwired in.

	/// </summary>

	public abstract Type ClassType  { get; }


	#region abstract methods
	/// <summary>

	/// Example factory method. Is bound to the constructor for ''ClassType'' with the same parameter signature.

	/// </summary>

	public abstract Demo CreateInstance( );


public class Demo {}
Then to automatically create factory classes for any derivitives of ''Demo'' a MetaClassFactory object is used. Constructing a MetaClassFactory binds it to the type of the base class to wrap and the base abstract factory class/interface(supports both!). A public method ''GenerateMetaClass'' takes a type and generates the meta/factory class for that type if it is a valid class to wrap. A call to ''GenerateMetaClasses'' (bunch of overloads control exactly were it searches for classes to wrap), scans for classes which inherit from the base class and generate meta/factory classes for them. There are options which control if the base class is included went generating the meta/factory class. Also the name of the ''ClassType'' property is alterable, as well as what is considered the factory method. Note: I havent yet settled on the exact terminology of ''meta class'' or ''factory class''. I plan on having a way to set properties in the factory class via attributes found on the class to be wrapped, which allows identification base on arbitary data to be in the client''s classes control. However, injecting custom compile-time validated code into the meta/factory class might be imposible. This can be circumvented by detecting went the user has defined the factory class but still wants the factory methods + other automated stuff handled for him, by tagging runtime constructed meta/factory classes with a custom attribute(Thus preventing requiring to write inout attribute tags on custom meta/factory classes). Any comments? I''m also planning on using the code caching architecture for a scripting plugin system which I''m planning on writing for Axiom (load & compile a bunch of .NET scripts and cache the results)

Share this post

Link to post
Share on other sites