Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Generate code for multiple languages using CodeDom
 

Generate code for multiple languages using CodeDom

Hi all,

I am developing a windows form control in C#. and i had to write a custom code serializer for one of its property.

The custom code serializer works well if I use C# langauge. But, if I use C++ development environment and drag-and-drop the control to the form, the code generated by the designer does not compile.

The problem is with the Scope-Resolution operator and Pointer-To-Member operator. I am serialyzing the operator by hardcoded string constants. (i.e. ".").

The C# code would look something like,

NamespaceX.NamespaceY.ObjectX x = new NamespaceX.NamespaceY.ObjectX();
x.Y = 100;

The C++ generated code would look like,

NamespaceX::NamespaceY::ObjectX x = new NamespaceX.NamespaceY.ObjectX();
x.Y = 100;

where as the correct code should be,
NamespaceX::NamespaceY::ObjectX x = new NamespaceX::NamespaceY::ObjectX();
x->Y = 100;


So, the question is,
i) Is there a way to find the language while serialyzing OR
ii) Serialize the correct ScopeResolution and PointerToMember operators specific to the language.


Thanks a million,
KillerX
KillerX  Tuesday, February 10, 2009 5:08 AM
Serialization (in .NET terms) doesn't involve code generation. Are you using the CodeDOM to generate code or what? Could you possibly post some of the code you are trying to get right.

Michael Taylor - 2/10/09
http://p3net.mvps.org
TaylorMichaelL  Tuesday, February 10, 2009 2:46 PM
Hi Michael,

Thanks for the reply.

I am not writing a OBJECT Serializater. But implementing Code Generation for WindowsForm control at Design-Time using a Custom CodeDomSerializer.

Here is what I have.
  • 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<double>.
  • The ObjectXXX has a internal field of type ObjectYYY.
  • The ObjectXXX should be initialized by passing Collection<double> 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?
KillerX  Friday, February 13, 2009 5:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Controls not visible while moving them on form.
• Tooltip gone mad!
• RightToLeft support in PropertyGrid
• Me.components = New System.ComponentModel.Container duplicate entry
• Different form views on one form
• Custom Scrolling
• Obtain the control beneath the cursor
• How to add Sort Icon to Data Table(Column) in windows aplication?
• Can sub-properties be hidden from the property grid?
• Applying DesignerSerializerAttribute to interface: abstractive UI