Thursday, September 23, 2004

This is a preview of a new language for Reflector that outputs to Refly (Refly is wraps up and simplifies the use of CodeDom).

What does the language do ?

The language creates Refly code. If the Refly code is executed it will generate the original code. This is particularly usefull to rapidely design CodeDom template: write the expected output, Reflectorize-it and get the Refly code. In the figure below, we see the code "cycle of life" with a decompiler and how it is modified if we add the "Refly" language.

A preview example

There is still a lot of work to do on the language but it can already generate the outline of classes. Let's see that on a simple class:

public class Original

{

private string name;

public Original()

{}

public string Name

{

get { return this.name;}

set { this.name = value;}

}

public void Run(string someParamter)

{

Console.WriteLine("{0}: someParameter");

}

public event EventHandler Click;

protected virtual void OnClick(EventArgs e)

{

if (this.Click != null)

this.Click(this, e);

}

}

After compiling and  loading in Reflector, the Refly language is outputting:

NamespaceDeclaration _dragAndDropUnitTesting = new NamespaceDeclaration("Original");

 

ClassDeclaration _original = _dragAndDropUnitTesting.AddClass("Original");

 

// Fields

FieldDeclaration name = _original.AddField(typeof(System.String), "name");

FieldDeclaration Click = _original.AddField(typeof(System.EventHandler), "Click");

 

// Events

EventDeclaration _click = _original.AddEvent(typeof(System.EventHandler), "Click");

 

// Constructor and Methods

ConstructorDeclaration ctor0 = _original.AddConstructor();

ctor0.Attributes |= System.CodeDom.MemberAttributes.Assembly;

ctor0.Attributes |= System.CodeDom.MemberAttributes.Family;

MethodDeclaration _run = _original.AddMethod("Run");

_run.Attributes |= System.CodeDom.MemberAttributes.Assembly;

_run.Attributes |= System.CodeDom.MemberAttributes.Family;

ParameterDeclaration _runsomeParamter = _run.Signature.Parameters.Add(typeof(System.String), "someParamter");

MethodDeclaration _onClick = _original.AddMethod("OnClick");

_onClick.Attributes |= System.CodeDom.MemberAttributes.Family;

ParameterDeclaration _onClicke = _onClick.Signature.Parameters.Add(typeof(System.EventArgs), "e");

 

// Properties

PropertyDeclaration _name = _original.AddProperty(typeof(System.String), "Name");

 

That's pretty ugly but not as much if you had to write CodeDom for that. We can compile and generate the code using the following instruction:

CodeGenerator gen = new CodeGenerator();
gen.GenerateCode(".", _dragAndDropUnitTesting);

Finally, the output of the execution is as follows:

using System;

/// <summary />

/// <remarks />

public class Original

{

/// <summary />

/// <remarks />

private string _name;

/// <summary />

/// <remarks />

private System.EventHandler _click;

/// <summary />

/// <remarks />

Original()

{

}

/// <summary />

/// <remarks />

public virtual string Name

{

}

/// <summary />

/// <remarks />

public event System.EventHandler Click;

/// <summary />

/// <remarks />

void Run(string someParamter)

{

}

/// <summary />

/// <remarks />

void OnClick(System.EventArgs e)

{

}

}

Of course, the result is far from behing perfect. Visibility handling sucks, statements are not reflected, etc... But sounds very promising to me :)

posted on Friday, September 24, 2004 2:30:00 AM UTC  #    Comments [11]
Tracked by:
"universalcard" (online) [Trackback]
"boredom solutions" (online) [Trackback]
Monday, June 06, 2005 5:25:17 PM UTC
Interesting...
Alf.
Monday, June 06, 2005 5:25:17 PM UTC
I'm keeping an eye on this work.
<br>
<br>A year or two ago I naively expected that I could build and manipulate .NET code from a program. (The idea has similarities to converting XHTML using XSLT.) When I looked at CodeDOM, I was intensely disappointed. Now I see what you are doing I have hope that eventually I may be able to tackle those projects again.
<br>
<br>Well done.
Mike Gale
Monday, June 06, 2005 5:25:17 PM UTC
CodeDom has several limitations (foreach? using? and I'm not mentionning more complex stuff) but it has a great power.
<br>
<br>The real problem about codedom is it's verbosity... that's why I came up with <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> which simplifies codedom use. But then, writing <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> code became too much work too...
<br>
Jonathan de Halleux
Monday, June 06, 2005 5:25:18 PM UTC
Nice one Jonathan... <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> is an extremely usefull wrapper for generating codedom code.
Jean Hibbert
Monday, June 06, 2005 5:25:18 PM UTC
:)
Jonathan de Halleux
Monday, June 06, 2005 5:25:18 PM UTC
Hi,
<br>
<br>I agree that <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> is an amazing library.
<br>But,
<br>
<br>I become depended on it.
<br>Can you shed some light about <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> future? Will you support CLR 2.0? (Generics and what ever)
<br>
<br>Will it be available with the source? Can you tell us when? (If it will)
<br>
<br>mordy.
<br>
<br>Can you send also to mail (mordy@magen.com)? - I work at a place with no internet. (But they forward the mail :-))
<br>
<br>Thnx
<br> mordy.
mordy shahar
Monday, June 06, 2005 5:25:18 PM UTC
can we give it a try ?
Pibos
Monday, June 06, 2005 5:25:19 PM UTC
It's not quite finished yet... I need to find the time polish it.
Jonathan de Halleux
Monday, June 06, 2005 5:25:19 PM UTC
how it's going with <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> janguage for <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a> ? it's a dead project ?
Pibos
Monday, June 06, 2005 5:25:19 PM UTC
The preview is available in the <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>.Framework addin. Load it and select <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> language.
<br>
<br>It is true that it has not evolved much since then.
Jonathan de Halleux
Monday, June 06, 2005 5:25:19 PM UTC
how can I add in the <a title="Helper wrapper around CodeDOM" href="http://mbunit.tigris.org" target="_blank">Refly</a> to the <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>
Arie
Comments are closed.