Dynamic class registration

Purpose of this page

The RtemplateEmbedder.cs described on this page is shown below.



RtemplateEmbedder.cs is a template to embed classes in your application.


Table of contents:


Class registration

Registration procedure

IEmbedPrototype

public void Embed(string path)

Fixed code

The file name is still "RtemplateEmbedder.cs". It's a good idea to rename the file to "RfakeEmbedder.cs" to avoid confusion.

Class registration

The only way to register a class with your application is a call to the AddBuiltIn() method of the RiceManager class.

public static void AddBuiltIn(string typename, PlainProtoTypeGetter pptg)

The first argument is a class name.

The second argument is the PlainProtoTypeGetter delegate.

public delegate Rtype PlainProtoTypeGetter();

For classes that are statically built into Cooker, the class name and delegate are known, so you can call the AddBuiltIn () method with the appropriate arguments. For example, registration for the Rbrowser class looks like this:

RiceManager.AddBuiltIn(Rbrowser.TYPENAME, Rbrowser.InstanceGetter);

Since the dynamic embedded class is an unknown class, neither the class name nor the delegate is known. It is not possible to register the class by calling the AddBuiltIn () method.

However, the RiceManager class has a method for registering unknown classes.

public static void RegisterClasses(string directoryPath)

Cooker uses this to perform dynamic class registration.


There are three rules for Cooker's dynamic embedded class registration.


1: Location

The .dll file for a dynamic embedded class must be located somewhere in the directory hierarchy rooted in the dlls subdirectory of the execution directory.

2: Name

It is described on the overview.

3: IEmbedPrototype interface

The .dll file for a dynamic embedded class requires an IEmbedPrototype derived class.

Registration procedure

Cooker calls the RegisterClasses(string directoryPath) method of the RiceManager class at startup. The argument is the path to the dlls subdirectory.

The RegisterClasses() method recursively traverses the directory hierarchy starting with the argument directory and looks for a .dll file with the appropriate name.

If it finds the .dll file, looks for the IEmbedPrototype derived class in it.

IEmbedPrototype

The IEmbedPrototype class is an interface class for dynamic class embedding.

1:public interface IEmbedPrototype {
2:void Embed(string path);
3:}

When the RegisterClasses() method finds an IEmbedPrototype derived class, it calls the Embed() method.

public void Embed(string path)

The Embed() method called by the above procedure is included in the project of the .dll file, it has understood the class name and the delegate of the class.

That is, we can call the AddBuiltIn() method to register the class.


When the Embed(string path) method is called, the absolute path of the .dll file is passed as the path argument.

Fixed code

We modify the template for the fake class.

1:

public class RfakeEmbedder : IEmbedPrototype {

2:

public void Embed(string path) {

3:

RiceManager.AddBuiltIn(Rfake.TYPENAME, Rfake.InstanceGenerator);

4:

}

5:

}

We are changing the class name and calling the AddBuiltIn() method with the name and delegate of the fake class.

Copyright © Cooker All rights reserved.