Проекты

.NET Compiler

Sometimes it happens that due to changes it is necessary to correct a small piece of code. But for this you have to recompile many assemblies, and the client has to wait for the next release. In order to avoid updating the entire solution for the sake of a couple of lines of code, a separate module was written that allows you to compile and save the source code at runtime.

The resulting assembly compiled in current app domain. Thanks to this, there is no problems with access to entire application. As the same time there is the option to mischief and break the application itself, so it's better to use it with caution. Accordingly when recompiling the code the old assembly remains in the current app domain.

User interface

Interface is available in the Tools→Compilers→CSC. In the left panel — the list of all loaded assemblies. Double clicking on the namespace will result adding this namespace to current class. Clicking to «Reference» button in toolbar the window for adding a new assembly to the project will open (Available options: GAC, File system, Loaded plugins).

Clicking on the button «Full Source» will result of showing full source code of the entire class. By default all surroundings except of the payload is hidden. If you want to hide all surroundings and revert to editing only payload, all additional code must be removed manually.

By clicking on button «Inclide Debug Info» we can add .pdb file to compiled assembly.

Next button is choosing compilation language and framework version. This parameters I trying to resolve manually that's why they can be not available in all machines.

Clicking «Compile» button you can invoke created method and check for compilation errors. List of all errors will be shown in the lower part of the window. While it is testing phase only methods without arguments are supported.

How to open compiler window from the code

To invoke the window for method editing you need to pass 2 arguments: Name of the new class that can be used from current plugin and current plugin identifier. This arguments will be stored in compiler plugin in save.

//Description of the caller plugin
IPluginDescription thisPlugin = this.PluginDescription;

//Key for the new method
String methodName = "GetSomeCookies";


IWindow window = this.HostWindows.Windows.CreateWindow("425f8b0c-f049-44ee-8375-4cc874d6bf94",
	"Plugin.Compiler.DocumentCompiler",
	false,
	new KeyValuePair<String, Object>("CallerPluginId", thisPlugin.ID),
	new KeyValuePair<String, Object>("ClassName", methodName));

How to compile and invoke method

To invoke this method after it was created and saved you need to pass the list of an arguments. For example let's imagine that our method accept one input argument with String type.

//Description of the caller plugin
IPluginDescription thisPlugin = this.PluginDescription;

//Key for a new method
String methodName = "GetSomeCookies";

Some input value for the GetSomeCookie(String) method
String value = "I want cookie";


IPluginDescription plugin = this.Window.Plugin.Host.Plugins["425f8b0c-f049-44ee-8375-4cc874d6bf94"];
Object result;

if(plugin == null) {
	this.Trace.TraceEvent(TraceEventType.Warning, 5, "Compiler plugin '425f8b0c-f049-44ee-8375-4cc874d6bf94' is not installed");
} else
{
	IPluginMethodInfo member = plugin.GetMember("InvokeDynamicMethod");
	if(member == null)
		throw new ArgumentNullException("Method 'InvokeDynamicMethod' is not found in the plugin '425f8b0c-f049-44ee-8375-4cc874d6bf94'");
	result = member.Invoke(
		thisPlugin,
		methodName,
		new Object[] { value, });
}
Теги:

Скачать

Ссылки

Родительские файлы

Дочерние файлы