Click here to Skip to main content
Click here to Skip to main content

Refly, makes the CodeDom'er life easier

By , 1 Mar 2004
 

Be constructive, comment your votes...

Introduction

With CodeDom, .NET has given us a flexible tool for generating source code in a variety of languages. Unfortunately, CodeDom has a major shortcoming, it is quite "verbose": Generating a little piece of code usually means writing dozens of CodeDom instructions.

This article presents Refly, a smart wrapper that should dramatically simplify code generation with CodeDom. The major features of Refly are:

  • Smart naming of the members, fields and properties,
  • Easy creation of namespace, classes, ...
  • Output to separate file for each class, with directories mapping the namespaces
  • Intuitive syntax for building expressions. For example,
    this.data.Close();

    is generated by:

    Expr.This.Field("data").Method("Close").Invoke();
  • Static helpers for common statements:
    if (value==null)
        throw new ArgumentNullExpression("value");

    is generated by the static method:

    Stm.ThrowIfNull(value);
  • Documentation writing helpers
  • Built-in templates: strongly typed collection, dictionary, data reader
  • ...

We will start with a comparative example on CodeDom and Refly and then, give a how-to on using Refly.

Comparative example: User class generation

Suppose that we want to create the following simple User class:

namespace Refly.Demo
{
public class User
{
    private string name;

    public User(string name)
    {
       this.name = name;
    }

    public String Name
    {
        get
        {
            return this.name;
        }
    }
}
}

CodeDom solution

Here comes a part of the CodeDom code to generate the above class (skip this if you know CodeDom):

// creating the Refly.Demo namespace
CodeNamespace demo = new CodeNamespace("Refly.Demo");

// create the User class
CodeTypeDeclaration user = new CodeTypeDeclaration("User");
user.IsClass = true;
demo.Types.Add(user);

// add name field
CodeMemberField name = new CodeMemberField(typeof(string),"name");
user.Members.Add(name);

// add constructor
CodeConstructor cstr = new CodeConstructor();
user.Members.Add(cstr);
CodeParameterDeclarationExpression pname = 
    new CodeParameterDeclarationExpression(typeof(string),"name");
cstr.Parameters.Add(pname);

// this.name = name;
CodeFieldReferenceExpression thisName = new CodeFieldReferenceExpression(
    new CodeThisReferenceExpression(),
    "name");
CodeAssignStatement assign = new CodeAssignStatement(
    thisName,
    pname
    );
cstr.Statements.Add(assign);

// add property
CodeMemberProperty p = new CodeMemberProperty();
p.Type=name.Type;
p.Name = "Name";
p.HasGet = true;
p.GetStatements.Add(
    new CodeMethodReturnStatement(thisName)
    );

Refly solution

Here comes the Refly version of the above. Although you do not know yet Refly, you will see that it is similar in many ways to CodeDom:

// creating the Refly namespace
NamespaceDeclaration demo= new NamespaceDeclaration("Refly.Demo");

// create the user class
ClassDeclaration user = demo.AddClass("User");

// add name field
FieldDeclaration name = user.AddField(typeof(string), "name");

// add constructor
ConstructorDeclaration cstr = user.AddConstructor();
// add name parameter
ParameterDeclaration pname = 
    cstr.Signature.AddParam(typeof(string), "name",true);

// this.name = name;
cstr.Body.AddAssign( 
    Expr.This.Field(name),
    Expr.Arg(pname)
    ); 

// add property
user.AddProperty(name, true, false, false);

Comparison

At first look, the two versions do not look very different, let me point out some interesting differences:

  • Add instances into their parents: in CodeDom, it is your job to add the new instance into their parents. In Refly, this is done by the framework. Therefore, you avoid forgetting to add the type into the parent.
  • Expression and Statement construction: this.name = name;. Expression and statement building has been highly simplified in Refly as you can see it in this 1 line example. Imagine the economy of work when you need to generate methods with dozens of lines.
  • Smart helpers: wrapping a field by a property is a usual task. This is done automatically by Refly.
  • Output: (not shown in the example), Refly will create the directory structure mapping the namespaces and output a file for each class/enum.

This concludes the introductory comparison example. The full source of the example is available in the demo.

Refly Architecture

The Refly framework is organized in the following components:

  • Refly.CodeDom, root namespace for the CodeDom generator,
  • Refly.CodeDom.Collections, useful strongly typed collections,
  • Refly.CodeDom.Expressions, expression wrappers,
  • Refly.CodeDom.Statements, statement wrappers,
  • Refly.CodeDom.Doc, documentation writing helpers,
  • Refly.Templates, some generator classes,
  • Refly.Xsd, XSD refactoring tools

Refly acts as a wrapper around CodeDom: each class of the CodeDom namespace has its Refly counter part. When Refly is asked to generate the code, it creates the CodeDom code and lets CodeDom generate the code.

Usually, importing the Refly.CodeDom is sufficient to have all the functionalities of Refly.

Generating code

This section gives step-by-step instructions to get you code generator running.

Creating a namespace

First of all, we need a namespace, that is an instance of NamespaceDeclaration:

NamespaceDeclaration ns = new NamespaceDeclaration("MyNs");

Classes and enums can be added to this namespace. Sub-namespaces can also be added to namespaces.

Adding a class

The NamespaceDeclaration class provides a method AddClass to create a new class:

ClassDeclaration user = ns.AddClass("user");

You do not need to worry about the naming case, Refly will "recase" the name you provided. Now that we have an empty type, we can test the generator.

Generating code

Refly provides the CodeGenerator class that takes care of creating the directories, calling CodeDom generator for each type in the namespace:

CodeGenerator gen = new CodeGenerator();
gen.GenerateCode("outputPath",ns);

By default, CodeGenerator will create C# code, but of course, you can change the output language by providing another provider:

// changin output to VB
gen.Provider=CodeGenerator.VbProvider;

Adding members

Back to our user class, you can add fields, methods, etc... by using the proper methods of ClassDeclaration:

// add field name
FieldDeclaration name = user.AddField(typeof(string),name);
// add Check
MethodDeclaration check = user.AddMethod("Check");
...

The signature of members (when it applies) is controlled by a MethodSignature through the Signature property. This object lets you add parameters and set the return type of the method.

// adding age to the check method
ParameterDeclaration age = check.Signature.AddParam(typeof(int),"age");

Adding comments

Each declaration type has a Documentation instance that can be used to create comments:

age.Doc.Summary.AddText("user age");

Building Expressions

The class Expr contains a number of static helper classes that will generate all the expression objects for you. Here are some code samples, with their Refly counter part:

  • this.name -> Expr.This.Field(name); where
    • Expr.This returns the this instance
    • .Field retrieves the field from an expression
  • this.Check(10) -> Expr.This.Method(check).Invoke(Expr.Prim(10)); where
    • .Method retrieves the method,
    • .Invoke invokes the method with the arguments,
    • Expr.Prim creates an expression from a primitive value (int, string, double, etc...)
  • ...

The Expr contains other methods to map typeof, new, cast, ...

Building Statements

As for expressions, the Stm class contains a lot of static helper classes to simplify your work. For example, simple statements such assign, return:

  • left = right; -> Stm.Assign( left, right);
  • return value; -> Stm.Return( value );
  • if (value==null) throw new ArgumentNullException("value"); -> Stm.ThrowIfNull(value);
  • Throw new Exception(); -> Stm.Throw(typeof(Exception));

More complex statements like if, for, try/catch/finally are also created with Stm. Refly also emulates foreach, which is not supported by CodeDom, by expanding the foreach as the C# compiler would be (using an enumerator, etc...).

More built-in examples

The Refly.Templates contains various generators for strongly-typed collection, dictionary and IDataReader wrapper. They are good starting point to get a grip on Refly. As a final example, this method generates a strongly typed dictionary (KeyType is the key type, ValueType is the value type):

NamespaceDeclaration ns = ...;
ClassDeclaration col = ns.AddClass(this.Name);

// set base class as CollectionBase
col.Parent = new TypeTypeDeclaration(typeof(DictionaryBase));

// default constructor
col.AddConstructor();

// add indexer
IndexerDeclaration index = col.AddIndexer(
    this.ValueType
    );
ParameterDeclaration pindex = index.Signature.AddParam(KeyType,"key",false);
// get body
index.Get.Return(
    (Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)).Cast(this.ValueType)
    )
    );

// set body
index.Set.AddAssign(
    Expr.This.Prop("Dictionary").Item(Expr.Arg(pindex)),
    Expr.Value
    );
            
// add method
MethodDeclaration add = col.AddMethod("Add");
ParameterDeclaration pKey = add.Signature.AddParam(this.KeyType,"key",true);
ParameterDeclaration pValue = add.Signature.AddParam(this.ValueType,"value",true);
add.Body.Add(
    Expr.This.Prop("Dictionary").Method("Add").Invoke(pKey,pValue)
    );

// contains method
MethodDeclaration contains = col.AddMethod("Contains");
contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));
ParameterDeclaration pKey = contains.Signature.AddParam(this.KeyType,"key",true);
contains.Body.Return(
    Expr.This.Prop("Dictionary").Method("Contains").Invoke(pKey)
    );

// remove method
MethodDeclaration remove = col.AddMethod("Remove");
ParameterDeclaration pKey = remove.Signature.AddParam(this.KeyType,"key",true);
    remove.Body.Add(
    Expr.This.Prop("Dictionary").Method("Remove").Invoke(pKey)
    );

The last bonus

Refly contains a last bonus, XsdTidy, that refactors the output of the Xsd.exe tool to nicer and easier to use classes.

History

  1. 2-03-2004, First release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1member[CC]22-Oct-12 22:42 
No download link, not a self-contained article as the source is missing, CodeProject should disallow to publish articles that have external source code links and become "useless" once the author stops caring.
Questionhow to user AttributeDeclaration [modified]memberwuhuacong22-Mar-12 5:12 
no AttributeDeclaration sample, could you tell me how to use the object?
How to implement the following code?
 
[Browsable(bool)] // this property should be visible
[ReadOnly(true)] // but just read only
[Description("sample hint1")] // sample hint1
[Category("Category1")] // Category that I want
[DisplayName("Int for Displaying")] // I want to say more, than just DisplayInt
public int DisplayInt
{
get { return m_DisplayInt; }
set { m_DisplayInt = value; }
}

modified 22-Mar-12 11:32am.

GeneralExtension Methodsmemberslifer1226-Jan-11 17:36 
Hey,
I want to implement extension methods using Refly but can't figure out a way to do so.
Any help in that direction is greatly appreciated.
Thank you
QuestionLatest version?memberMichael P Butler26-Aug-06 9:49 
http://www.dotnetwiki.org/ can't be accessed at the moment, is there anywhere else I can get the latest version?
 

 

AnswerThis version seems newermemberMichael P Butler28-Aug-06 8:29 
http://refly.tigris.org/source/browse/refly/[^]
 

GeneralRe: This version seems newermemberZac Greve6-Mar-12 5:54 
No code there. Frown | :(
Attempting to load signature...
A NullSignatureException was unhandled.
Message: "No signature exists"

GeneralHelpmemberHayseeds12-Jul-06 13:49 
Okay this is driving me nuts. I can't seem to figure out how to do the following.
 
string x = "a";
string y = "b";
 
if (x == y)
{
..
..
..
}
 

QuestionEnums and NamespacesmemberManuel Salvatore18-May-06 6:56 
Hi, i found your project very usefull, i wish to do some improvements, i got some ideas
but my issue is, i noticed that the enum declaration fall in troubles while generating, this because, the enum field is written Camelized,
but the when I use Expr.Type("enum decalaration").Field("name") to get a reference to that enum, the enum field reference is generated not camelized, instead Capitalized... what can i do?? what happen??
 
second, why when I add a namespace to a namespace, ex:
 
NamespaceDeclaration ns1 = new NamespaceDeclaration("Application");
ClassDeclaration cl1 = ns1.AddClass("Class1")
NamespaceDeclaration ns2 = ns1.AddNamespace("Services");
ClassDeclaration cl2 = ns2.AddClass("Class2")
 
i expect that in the output, classes under the ns1 should be written:
namespace Application
{
class Class1
....
 
and classes under the ns2 should be written:
namespace Application.Services
{
class Class2
....
 
instead, Class1 is under th namespace 'Application', and Class2 is under 'Service'!
i took a look at your code, and i found the FullName property returns cotrrectly namespace.name
but in the code generation is not used the FullName property, instead the sole Name is used
what can i do??
 
if u can give me some instruction, i can fix by myself the problem
Many thanks, about the great job, and about hear me now
Cya
 
Manuel Salvatore Martone
GeneralBase args for constructormemberdj peregrine19-Feb-06 12:27 
I don't know about the CVS version, but I needed my constructors to have a baseArgs. I added the code( its only a few lines of code). So I have two questions:
 
1)Do you mind if I don't upload the source code with my app, and just the DLL.
 

GeneralRe: Base args for constructormemberjscote3-Mar-06 2:36 
Can you please provide me with the solution? I have the same problem and would like to avoid reinventing the wheel.
 
Thanks a lot
GeneralRe: Base args for constructormemberdj peregrine3-Mar-06 14:53 
Its quite simple:
 
USAGE:
 
ConstructorDeclaration construct=sq.AddConstructor();
construct.Signature.AddParam(typeof(string),"wordup",true);
construct.Signature.AddParam(typeof(int),"yo",true);
construct.AddBaseArgs( new string[]{"wordup","yo"} );
 
generates in C# :
 
public foo(string wordup,int yo):base(wordup,yo){
 
}
-------------------------------------------------------------------
Here is my ConstructorDelecaration.cs ( under the codedom folder).
--------------------------------------------------------------------
 

using System;
using System.CodeDom;
 
namespace Refly.CodeDom
{
using Refly.CodeDom.Collections;
///
/// Summary description for ConstructorDeclaration.
///

public class ConstructorDeclaration : Declaration
{
private Declaration declaringType;
private MethodSignature signature = new MethodSignature();
private StatementCollection body = new StatementCollection();
private CodeExpressionCollection baseargs=new CodeExpressionCollection();
internal ConstructorDeclaration(Declaration declaringType)
:base("",declaringType.Conformer)
{
this.declaringType = declaringType;
this.Attributes = MemberAttributes.Public;
}
 
public MethodSignature Signature
{
get
{
return this.signature;
}
}
internal CodeExpressionCollection Baseargs
{
get
{
return this.baseargs;
}
set
{
this.baseargs=value;
}
}
public void AddBaseArgs(string basearg){
baseargs.Add( new CodeVariableReferenceExpression(basearg) );
}
public void AddBaseArgs(string[] strings){
foreach(string s in strings)
baseargs.Add( new CodeVariableReferenceExpression(s) );
}
public CodeConstructor ToCodeDom()
{
CodeConstructor ctr = new CodeConstructor();
ctr.BaseConstructorArgs.AddRange(baseargs);
ctr.Name = this.Name;
base.ToCodeDom(ctr);
 
this.Signature.ToCodeDom(ctr);

// base call
 
// body
if (this.body!=null)
{
this.body.ToCodeDom(ctr.Statements);
}
 
return ctr;
}
 
public StatementCollection Body
{
get
{
return this.body;
}
}
}
}
 

GeneralProject cannot be builtmemberdirc15-Nov-05 4:31 
Tried downloading from wikidotnet.org.. or whatever it was but the site is not available.
 
Downloaded the demo, but could not build because there are a set of files under bin/debug/CS/Collections that are not available.
 
Dirc
AnswerRe: Project cannot be builtmemberDemeter3-Apr-08 0:41 
One way: Exclude these files from project, then build it Smile | :)
 
Demeter

QuestionHow to add a region block?memberwolfsecond24-Jun-05 20:31 
Hey, it's really a cool job!
but I wonder how a add region block,like this
#region something
#endregion
 
it'll make code mush easier to read, can refly do that?
 
wolfsecond
AnswerRe: How to add a region block?memberJonathan de Halleux24-Jun-05 20:33 
Region support was added in .Net 2.0. It is not supported by previous versions of CodeDom.
 
Jonathan de Halleux - My Blog
GeneralCode quotationssussKamil Skalski19-Dec-04 23:37 
With Nemerle (http://nemerle.org) you can generate code much more simplier - http://nemerle.org/macros.html
 
You would just write
def classcode = <[ decl:
   public class A : IMy {
      pubic field1 : int;
      f () : void { }
   }
]>;
 
to generate a class. The generated code is in most cases used at compile-time to enrich the program being compiled (http://nemerle.org/macrouse.html)
GeneralRe: Code quotationsmemberJonathan de Halleux24-Jun-05 20:36 
You obviously missed the primery advantage of CodeDom which lets you output code in any .Net language (as long as it has a codedromprovider). i.e the same generator can produce C#, VB or C++.
 
You approach is a template based and I would suggest readers to have a look at CodeSmith as well for such approach.
 
I'm not saying your approach is not good, I'm saying it has also disavantages.
 
Jonathan de Halleux - My Blog
GeneralGreat but still struggling to make use of reflymembervbnetuk6-Dec-04 2:32 
Hi,
First all i would to say that your refly seems very good.
However I am struggling with few bits and bobs in making it to work.
What i am trying to say is do you have a testHarness where you test all the things you have written?
The reason i am saying this is because even though and i m sure your wrapper is easier than the codedom ,lack of documentation is preventing me to exploit the full potential of your dll and its making me waste more time than the codedom itself
 
I understand that you might not have the time or you never needed to write a testharness but if you did have one could you share it?
I need to build properties with some validation in the set code in vb.net
Plus some methods with a loop etc.I also would like to generate VB.net Collections.
I am sure you can handle of that with your refly.I cant figure it out
 

I appreciate any feedback
 

 
Thanks A lot
vbnetuk@yahoo.co.uk
GeneralHelp on Object Arraysmemberterence wallace8-Oct-04 18:51 
I can not figure out for the longest time how to write and object array using Refly. Examine the following:
 
VB:
Dim rows As Children()
 
Function GetChildren() as Children()
...
End Function
 
Can you provide me with clues on how to do this using refly? Thanks. OMG | :OMG:
GeneralRe: Help on Object ArraysmemberJonathan de Halleux24-Oct-04 9:54 
You can't... they were not supported.
 
I've added support for Arrays in the latest binary which is available in the TestDriven.Net installer ( http://www.testdriven.net/wiki ). Make sure you get the latest automated build (>804).
 
The array can now be create using Expr.NewArray and index can be accessed using Expression.ArrayItem
 
Cheers,
Jonathan
 
Jonathan de Halleux - My Blog
GeneralRe: Help on Object ArraysmemberWallace.TL25-Oct-04 7:32 
Dear Jonathan,
 
TestDriven really sounds nice and it must truly be something that you are excited about. I think that we all understand that.However, TestDriven is absolutely useless to us here.
 
The Refly source code on the other hand is revolutionary and I thank you for your work in that regard. How can we get a copy of that latest version of Refly 'only'.
 
TL Wallace
http://www.arkitechebc.com
GeneralRe: Help on Object ArraysmemberJonathan de Halleux25-Oct-04 19:09 
Humm, it is much more convinient for me to ship everything in 1 single installer (QuickGraph, Refly, TestFu, MbUnit).
 
Is it a problem to install TD.NET ? The refly dll is located at C:\Program Files\TestDriven.Net\MbUnit\Refly.dll and it is totally independent from TD.NET
 
Would this be a solution ?
 
Jonathan de Halleux - My Blog
GeneralDownloading latest version.memberMotoMan04516-Aug-04 19:00 
I went to www.dotnetwiki.org to down load the last sources, and I did not find
a obvious download for Refly, and the Refly donwload page on your blog is is broken. I then tried to download the mbunit project because it sounds like you have Refly as part of the mbunit cvs project, but I didn't get any sources.
Anyway, could you point me in the diection on how to get the lastest sources.
I'm looking to build some sql store procs wrappers, and Refly is exactly what I'm looking for.
 
Terry.
GeneralRe: Downloading latest version.memberJonathan de Halleux16-Aug-04 21:24 
Refly is bundled with MbUnit. You can get the latest build in the MbUnit 2.20 download Smile | :) Have fun and feedback if you got problem or if there are missing features Smile | :)
 
Jonathan de Halleux - My Blog
GeneralRe: Downloading latest version.membervbnetuk3-Dec-04 4:04 
Hi there,
 
Your refly is fantastic.i cannot find the latest source code download for this project .Could you point a link?
 
Also you wrote so much stuff and you made it easier do you have some more example how to use it.
I my case i want to build custom property with validation and so on and i trying to figure out how to do it with refly.
 
Thanks for sharing this
 

 
vbnetuk@yahoo.co.uk
GeneralRe: Downloading latest version.memberdojohansen17-Oct-07 5:28 
1) Where can we download the MbUnit build then?
 
2) Is it not possible to download this in source form somewhere?
 
None of the URLs posted here seem to work; the dotnetwiki.org site doesn't respond, the refly.tigris.org link responds but there is no code in the CVS, and the SourceForge project only contains the DLL.
 
It looks like a great wrapper that can save much coding effort, but unfortunately it is missing XmlDoc or other documentation and I find myself having trouble figuring out how to do something as simple as declaring a public field.
 
I can of course make private fields for storage and getter-setter properties for access, but it seems to me I should be able to generate the code I want to...?
GeneralRe: Downloading latest version.memberDavidSchmitt19-Nov-09 23:51 
The latest MbUnit release that contains XsdTidy (that I could find) is 2.4.2: http://www.mbunit.com/Downloads.aspx[^]. The source can be found in the google code project: http://code.google.com/p/mb-unit/source/browse/#svn/trunk/v2/src/refly/XsdTidy[^].
 
It doesn't seem to be carried over to the 3.0 branch. Perhaps a separate project should be forked. Peli?
GeneralGreat Job! A few issues though..sussPhilip Laureano13-Jun-04 19:11 
Hi Jonathan,
 
First of all, thanks for writing Refly--I think you did a great job, and it really made my CodeDom code much easier to read and write. I did run into a couple of issues though, and I had to make a few changes to some of the classes to accommodate those issues. These are:
 
#1. The CodeGenerator class only supports file output. Is there anyway we could have it output to an in-memory object such as string or a textwriter-derived object?
 
#2. The NamespaceDeclaration class only supports one class per namespace. Do you have any plans to add support for having multiple classes in the same namespace?
 
Anyway, if you're interested (and want to swap a few notes), I've added some changes to Refly that affect those issues above, so let me know. Smile | :)
 

GeneralRe: Great Job! A few issues though..memberJonathan de Halleux13-Jun-04 19:27 
Hi,
 
well, the refly version on CodeProject is seriously outdated. If you want the latest bits, you can go to http://mbunit.tigris.org [^]. Refly is the CVS.
 
1)That's true. Please send patch Smile | :)
Even, better, want to contribute to the CVS ?
2) I don't understand this. That's false. Namespace declaration supports multple class per namespace ????
 

 
Jonathan de Halleux - My Blog
GeneralImporting existing classesmemberjogster226-May-04 6:38 
Hi,
Is there anyway to import an existing class that is compiled and then spitting it out into the new namespace on a code generation.
 
I would expect to add the type existing in the current assemly to appear in the target namespace this is essentially a framework class that only uses interfaces to provide some glue logic. The instance specific classes I can generate using refly. Admittedly I could genrate the classes by defining explicit templates, but I just wondered if there was an easier way as there is no paramatization of these classes.
 
->jogster
GeneralRe: Importing existing classesmemberJonathan de Halleux9-Jun-04 3:18 
Check out reflector ( http://www.aisto.com/roeder/dotnet/ )
 
Jonathan de Halleux - My Blog - www.dotnetwiki.org -
MbUnit - QuickGraph - NCollection

GeneralNameConformer.ToCamelsussSteve Willcock26-Mar-04 6:34 
First, just wanted to say that this is a fantastic project and it has helped me to speed up my CodeDom work immensely - I had started to write some wrapper classes myself but then I found this and it has just about everything I needed - thanks Jonathan! Smile | :)
 
I've had some issues with the NameConformer.ToCamel method - it seems to put odd upper case chars into the field names. Using the ClassDeclaration.AddField method, I got field names like the following coming out:
 
_manAgerid (I put in managerID)
_notEs (I put in notes)
 
I haven't looked too closely at why this happened, I just modified the code to bypass the call to NameConformer.ToCamel in the AddField method (I already 'Camelised' the names anyway before putting them in, and I'm only using C# at the moment so I don't need the leading _)
 
Thanks!
 
Steve
GeneralRe: NameConformer.ToCamelmemberJonathan de Halleux26-Mar-04 6:50 
Yep, name conformer is really crap. I've improved it a bit but it is on my machine. I'll need to update that later on....
 
Thanks for the support Smile | :)
 
Btw, Refly is going to be hosted on mbunit.tigris.org[^] so if you want to continue to contribute, you're welcome.
 
Jonathan de Halleux - www.dotnetwiki.org -
MbUnit - QuickGraph

GeneralRe: NameConformer.ToCamelsussSteve Willcock26-Mar-04 7:07 
Ok thanks I'll keep an eye on mbunit.tigris.org
QuestionHow to generate a while block?memberjscote16-Mar-04 10:03 
I'm trying to generate a while block and it seems that the IterationStatement class can only generate a for loop. Looking at Microsoft documentation, it seems possible to generate a while loop but according to some research on the internet, it seems to be a limitation of CodeDOM.
 
So is it possible?
 
JS
AnswerRe: How to generate a while block?memberJonathan de Halleux26-Mar-04 6:49 
While is just a syntax sugor for
 
for( ; iteration_test ; )
{
...
}
 

The Stm.While method does that for you.
 
Jonathan de Halleux - www.dotnetwiki.org -
MbUnit - QuickGraph

QuestionHow can I...?memberjscote12-Mar-04 14:53 
I would like to know if there is a way to use a class that has just been declared (or generated) with relfy as a type for a parameter.
 
For instance, I created two ClassDeclaration object. To the second object, I add a method for which the return value should be the fist object type.
 
How can I do that?
 
Thanks
 
JS
AnswerRe: How can I...?memberjscote13-Mar-04 6:04 
It seems that I found the solution.
 
You can use the following:
 
MethodDeclaration method = class2.AddMethod("TestMethod");
method.Signature.ReturnType = new StringDeclaration(Class2.Name);
 
JS
GeneralRe: How can I...?memberJonathan de Halleux15-Mar-04 19:56 
doesn't ths work ?
 
method.Signature.ReturnType = Class2; ?
 
Jonathan de Halleux.

www.dotnetwiki.org

Sign In·View Thread·Permalink
GeneralRe: How can I...?memberjscote16-Mar-04 0:56 
Yes method.Signature.ReturnType = class2; works just fine. I don't know why I had problem with it in the first place.
 
Thanks
 
JS
GeneralCool! A question though...membermsmits12-Mar-04 4:06 
Very nice package. Only regret I have is that I've been building something similar in the last days, and this is far more comprehensive. Wish I'd seen this earlier.
 
A question though, I am missing a way to create a CodeTypeConstructor. Is there any?
 
Thanks,
Michel
GeneralRe: Cool! A question though...memberJonathan de Halleux15-Mar-04 19:52 
msmits wrote:
A question though, I am missing a way to create a CodeTypeConstructor. Is there any?
 
You cannot create directly (this is by design). You must first create a NamespaceDeclaration and then add a class, event, etc...
 
Jonathan de Halleux.

www.dotnetwiki.org

Sign In·View Thread·Permalink
GeneralGood job!memberFrank Hileman5-Mar-04 17:19 
Looks much easier than raw codedom. I may even have a use for this...Smile | :)
Thanks!
 
- Frank

 
check out VG.net: www.vgdotnet.com
An animated vector graphics system integrated in VS.net
GeneralRe: Good job!memberJonathan de Halleux7-Mar-04 1:23 
Thanks, and congrats fro vg.net very nice appSmile | :)
 
Jonathan de Halleux.

www.dotnetwiki.org

GeneralCode GenerationmemberBen Swann3-Mar-04 4:34 
CodeDom itself it quite a lengthy process of generating code.
Refly from the article looks a lot cleaner, so I imagine there may be soom limitation. However, I am not discussing that issue, considering I have not looked at the code properly.
 
Currently I think its quicker to define a class hierarchy in the form of XML. Then run XSLT scripts against the XML to generate the code. You can write a wrapper in .NET, and select the stylesheet in accordance with what language you want to generate the class for.
GeneralRe: Code GenerationmemberJonathan de Halleux3-Mar-04 6:42 
Yes, generating code from XML is the big and most widely used alternative. CodeSmith or other tools have already done that.
 
To my sense, it is difficult to answer wheter xml generations is definitely better than CodeDom. Both have their own advantage to my opinion:
 
- xml templates are easier to edit, being less verbose, moreover you don't have the hassle of writing-compiling-looking at the result, since you can write and look at the output in real time.
 
- CodeDom benefits from the flexibility of runtime generated code. For instance, if you need to create "smart" strongly-typed collection, that will choose different type for storing data depending on constraints on this data (non null,etc..). (of course, you can generate xml at runtime and render it to what you like).
 
...
 
Jonathan de Halleux.

www.dotnetwiki.org

GeneralRe: Code GenerationmemberFrank Hileman5-Mar-04 17:23 
CodeDom is nice because it works with any supported language -- no need to write separate templates or stylesheets for each one.
 
Also, if you generate code within a CodeDomSerializer, that is the only way you are allowed to do it.
 

- Frank

 
check out VG.net: www.vgdotnet.com
An animated vector graphics system integrated in VS.net
QuestionHow to call static methods?memberaaron330i14-Jun-04 8:52 
I'm stuck trying to get Refly to generate code that calls static methods on framework classes like below:
 
System.Console.WriteLine("Hello World!");
 
I keep feeling like I'm getting close, but for some terribly odd reason, I'm running into a roadblock.
 

 

 
Aaron A. Anderson
blog: http://www.aaronthearchitect.com
AnswerRe: How to call static methods?sussAaron A. Anderson15-Jun-04 16:17 
I did get it to work by reverting to the old CodeDOM code, just thought there was an easier way to do it with Refly.
GeneralRe: How to call static methods?memberJonathan de Halleux15-Jun-04 21:15 
Could you post a snippet of CodeDom invoking a static method ? It would help to integrate that in Refly (I don't think Refly supports static calls now)...
 
Jonathan de Halleux - My Blog

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130617.1 | Last Updated 2 Mar 2004
Article Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid