views:

177

answers:

2

Hi All,

I am implementing Code Generation for WindowsForm control at Design-Time, using a Custom CodeDomSerializer.

Here is what I have.

  • A user control i.e. MyControl written in C#.
  • MyControl has a property MyControlProperty of type ObjectXXX that is accessible publicly.(like myControl.MyControlProperty).
  • The type ObjectYYY has a public property PropertyXXX of type Collection.
  • The ObjectXXX has a internal field of type ObjectYYY.
  • The ObjectXXX should be initialized by passing Collection (which is nothing but ObjectYYY.PropertyXXX).

The code generated should be as given in the code snippet below.

Line1. NamespaceX.NamespaceY.ObjectXXX x = new NamespaceX.NamespaceY.ObjectXXX(NamespaceX.NamespaceY.ObjectYYY.PropertyXXX);
Line2. myControl.MyControlProperty = x;

I succeeded in generating the aforementioned code at Design-Time by writing a custom CodeDomSerializer FOR C# Language.

But, if i use MyControl for developing an application in C++ Language, the DOT operator is serialized for both ScopeResolution and Pointer-To-Member operator.

What I am doing for code in Line1 is,

string fullyQualifiedName = "NamespaceX.NamespaceY.ObjectYYY.PropertyXXX"; // HERE VARIABLE NAME IS HARDCODED WITH TWO TYPES OF OPERATORS
CodeExpression[] parameters = new CodeExpression[] {new CodeVariableReferenceExpression(fullyQualifiedName);};
CodeStatement code = new CodeVariableDeclarationStatement(typeof(ObjectXXX), "objectXXX1", new CodeObjectCreateExpression(new CodeTypeReference(typeof(ObjectXXX)), parameters));
generatedCode.Add(code); //generatedCode has the final code

For Line2,

CodeExpression codeLhs = new CodeVariableReferenceExpression(myControlVariable + "." + "MyControlProperty"); // HERE Pointer-To-Member IS HARDCODED AS DOT
CodeExpression codeRhs = new CodeVariableReferenceExpression("objectXXX1");
CodeAssignStatement codeAssignStmt = new CodeAssignStatement(codeLhs, codeRhs);
generatedCode.Add(codeAssignStmt); //generatedCode has the final code

Obviously the C++ Designer generated code should have '::' operator(and not DOT) for the ScopeResolution and '->' for the Pointer-To-Member resolution. I was not able to figure out how to make the code serialization for any CLR supported language.

How to solve this problem?

-Thanks a bunch

Dattebayo

A: 

Not sure if this will help, but have you looked at MyGeneration its a multi language code generator written in .Net. It doesn't use CodeDomSerializer, but it does generate good code - maybe it'll solve your underlying problem without having to re-invent the wheel?

MrTelly
A: 

Thanks for the quick reply.

I found the solution.

What i need was generating code containing property access and generating code for of .NET types.

To generate code that accesses a property, one should use CodePropertyReferenceExpression. This solves my problem with Line2.

To generate code that contains a Type, one should use Code CodeTypeReferenceExpression. This combined with CodePropertyReferenceExpression solved problem with Line1.

Now, I am able to generate code properly w.r.t. the Language in use.

//For C# The code would be
NamespaceX.NamespaceY.ObjectXXX x = new NamespaceX.NamespaceY.ObjectXXX(NamespaceX.NamespaceY.ObjectYYY.PropertyXXX);
this.myControl.MyControlProperty = x;

//For C++ The code would be
NamespaceX::NamespaceY::ObjectXXX x = new NamespaceX::NamespaceY::ObjectXXX(NamespaceX::NamespaceY::ObjectYYY::PropertyXXX);
this->myControl->MyControlProperty = x;
dattebayo