Windows Develop Bookmark and Share   
 index > Windows Forms Designer > CodeDomSerializer and multidimensional array initialization
 

CodeDomSerializer and multidimensional array initialization

Anybody know why the CodeDomSerializer for the following property


public
ushort[][] ChannelAntennaMap
{
get;
set;
}

emits the erroroneous initializer below. The C# sharp provider says it supports arrays of array. Know of a workaround?

RDF1.ChannelAntennaMap =

new ushort[] {
((
ushort)(2)),
((
ushort)(4)),
((
ushort)(22))}};

This should be

RDF1.ChannelAntennaMap = new ushort[][] {
new ushort[] {
((
ushort)(1)),
((
ushort)(3)),
((ushort)(21))},

new ushort[] {
((
ushort)(2)),
((
ushort)(4)),
((
ushort)(22))}};

  • Edited byFM1965 Thursday, July 02, 2009 6:30 PM
  • Edited byFM1965 Thursday, July 02, 2009 6:31 PM
  • Edited byFM1965 Thursday, July 02, 2009 6:33 PM
  • Edited byFM1965 Thursday, July 02, 2009 6:33 PM
  •  
FM1965  Tuesday, June 30, 2009 10:09 PM

Hi FM1965,

I am sorry for the late reply. This is my code snippet:

private string GetCode()

{

CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

CodeCompileUnit unit = new CodeCompileUnit();

//Set namespace.

CodeNamespace nameSpace = new CodeNamespace("samples");

unit.Namespaces.Add(nameSpace);

nameSpace.Imports.Add(new CodeNamespaceImport("System"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Xml"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Collections"));

//Add class definition MyArray.

CodeTypeDeclaration arraryClass = new CodeTypeDeclaration("MyArray");

nameSpace.Types.Add(arraryClass);

//Add array field.

CodeTypeReference arrType = new CodeTypeReference(typeof(ushort[][]));

CodeMemberField arrField = new CodeMemberField();

arrField.Type = arrType;

arrField.Name = "_channelAntennaMap";

arrField.Attributes = MemberAttributes.Private;

arrField.InitExpression = new CodePrimitiveExpression(null);

arraryClass.Members.Add(arrField);

//Add array property.

CodeMemberProperty arrProperty = new CodeMemberProperty();

arrProperty.Type = arrType;

arrProperty.Name = "ChannelAntennaMap";

arrProperty.Attributes = MemberAttributes.Public;

arrProperty.HasGet = true;

arrProperty.HasSet = true;

arrProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrField.Name)));

arrProperty.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrField.Name),

new CodePropertySetValueReferenceExpression()));

arraryClass.Members.Add(arrProperty);

//Add class definition MainClass.

CodeTypeDeclaration MainClass = new CodeTypeDeclaration("MainClass");

nameSpace.Types.Add(MainClass);

//Add a field of type MyArray.

CodeMemberField arrClassField = new CodeMemberField();

arrClassField.Type = new CodeTypeReference(arraryClass.Name);

arrClassField.Name = "RDF1";

arrClassField.Attributes = MemberAttributes.Private;

MainClass.Members.Add(arrClassField);

//Add a method, including the array initialization.

CodeMemberMethod InitArray = new CodeMemberMethod();

InitArray.Name = "InitArrary";

InitArray.Parameters.Add(new CodeParameterDeclarationExpression(arrType, "initialValues"));

MainClass.Members.Add(InitArray);

//Array initialization.

CodeArrayCreateExpression arrayInitial = new CodeArrayCreateExpression(arrType, 5);

CodeArrayCreateExpression[] values = new CodeArrayCreateExpression[5];

for (int i = 0; i < values.Length; i++)

{

values[i] = new CodeArrayCreateExpression(typeof(ushort), 2);

CodeCastExpression[] subValues = new CodeCastExpression[2];

for (int j = 0; j < subValues.Length; j++)

{

subValues[j] = new CodeCastExpression();

CodePrimitiveExpression val = new CodePrimitiveExpression(i * 5 + j);

subValues[j].Expression = val;

subValues[j].TargetType = new CodeTypeReference(typeof(ushort));

}

values[i].Initializers.AddRange(subValues);

}

arrayInitial.Initializers.AddRange(values);

//Array assign statement.

CodePropertyReferenceExpression refExp = new CodePropertyReferenceExpression();

refExp.PropertyName = arrProperty.Name;

refExp.TargetObject = new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrClassField.Name);

CodeAssignStatement arraryinit = new CodeAssignStatement(refExp,arrayInitial);

InitArray.Statements.Add(arraryinit);

//Save to file

string sourceFile = "MyClass.cs";

IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(sourceFile, false), " ");

provider.GenerateCodeFromCompileUnit(unit, tw, new CodeGeneratorOptions());

tw.Close();

//Return the text.

return File.ReadAllText(sourceFile);

}

This is the generated code snippet:
public class MyArray

{

private ushort[][] _channelAntennaMap;

public virtual ushort[][] ChannelAntennaMap

{

get

{

return this._channelAntennaMap;

}

set

{

this._channelAntennaMap = value;

}

}

}

public class MainClass

{

private MyArray RDF1;

private void InitArrary(ushort[][] initialValues)

{

this.RDF1.ChannelAntennaMap = new ushort[][] {

new ushort[] {

((ushort)(0)),

((ushort)(1))},

new ushort[] {

((ushort)(5)),

((ushort)(6))},

new ushort[] {

((ushort)(10)),

((ushort)(11))},

new ushort[] {

((ushort)(15)),

((ushort)(16))},

new ushort[] {

((ushort)(20)),

((ushort)(21))}};

}

}

You can get more information about CodeDom from

http://msdn.microsoft.com/en-us/library/system.codedom.aspx.

This is a sample:
http://msdn.microsoft.com/en-us/library/h82xde1t(VS.80).aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 03, 2009 6:28 AM

Hi FM1965,

I am sorry for the late reply. This is my code snippet:

private string GetCode()

{

CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

CodeCompileUnit unit = new CodeCompileUnit();

//Set namespace.

CodeNamespace nameSpace = new CodeNamespace("samples");

unit.Namespaces.Add(nameSpace);

nameSpace.Imports.Add(new CodeNamespaceImport("System"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Xml"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));

nameSpace.Imports.Add(new CodeNamespaceImport("System.Collections"));

//Add class definition MyArray.

CodeTypeDeclaration arraryClass = new CodeTypeDeclaration("MyArray");

nameSpace.Types.Add(arraryClass);

//Add array field.

CodeTypeReference arrType = new CodeTypeReference(typeof(ushort[][]));

CodeMemberField arrField = new CodeMemberField();

arrField.Type = arrType;

arrField.Name = "_channelAntennaMap";

arrField.Attributes = MemberAttributes.Private;

arrField.InitExpression = new CodePrimitiveExpression(null);

arraryClass.Members.Add(arrField);

//Add array property.

CodeMemberProperty arrProperty = new CodeMemberProperty();

arrProperty.Type = arrType;

arrProperty.Name = "ChannelAntennaMap";

arrProperty.Attributes = MemberAttributes.Public;

arrProperty.HasGet = true;

arrProperty.HasSet = true;

arrProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrField.Name)));

arrProperty.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrField.Name),

new CodePropertySetValueReferenceExpression()));

arraryClass.Members.Add(arrProperty);

//Add class definition MainClass.

CodeTypeDeclaration MainClass = new CodeTypeDeclaration("MainClass");

nameSpace.Types.Add(MainClass);

//Add a field of type MyArray.

CodeMemberField arrClassField = new CodeMemberField();

arrClassField.Type = new CodeTypeReference(arraryClass.Name);

arrClassField.Name = "RDF1";

arrClassField.Attributes = MemberAttributes.Private;

MainClass.Members.Add(arrClassField);

//Add a method, including the array initialization.

CodeMemberMethod InitArray = new CodeMemberMethod();

InitArray.Name = "InitArrary";

InitArray.Parameters.Add(new CodeParameterDeclarationExpression(arrType, "initialValues"));

MainClass.Members.Add(InitArray);

//Array initialization.

CodeArrayCreateExpression arrayInitial = new CodeArrayCreateExpression(arrType, 5);

CodeArrayCreateExpression[] values = new CodeArrayCreateExpression[5];

for (int i = 0; i < values.Length; i++)

{

values[i] = new CodeArrayCreateExpression(typeof(ushort), 2);

CodeCastExpression[] subValues = new CodeCastExpression[2];

for (int j = 0; j < subValues.Length; j++)

{

subValues[j] = new CodeCastExpression();

CodePrimitiveExpression val = new CodePrimitiveExpression(i * 5 + j);

subValues[j].Expression = val;

subValues[j].TargetType = new CodeTypeReference(typeof(ushort));

}

values[i].Initializers.AddRange(subValues);

}

arrayInitial.Initializers.AddRange(values);

//Array assign statement.

CodePropertyReferenceExpression refExp = new CodePropertyReferenceExpression();

refExp.PropertyName = arrProperty.Name;

refExp.TargetObject = new CodeFieldReferenceExpression(

new CodeThisReferenceExpression(), arrClassField.Name);

CodeAssignStatement arraryinit = new CodeAssignStatement(refExp,arrayInitial);

InitArray.Statements.Add(arraryinit);

//Save to file

string sourceFile = "MyClass.cs";

IndentedTextWriter tw = new IndentedTextWriter(new StreamWriter(sourceFile, false), " ");

provider.GenerateCodeFromCompileUnit(unit, tw, new CodeGeneratorOptions());

tw.Close();

//Return the text.

return File.ReadAllText(sourceFile);

}

This is the generated code snippet:
public class MyArray

{

private ushort[][] _channelAntennaMap;

public virtual ushort[][] ChannelAntennaMap

{

get

{

return this._channelAntennaMap;

}

set

{

this._channelAntennaMap = value;

}

}

}

public class MainClass

{

private MyArray RDF1;

private void InitArrary(ushort[][] initialValues)

{

this.RDF1.ChannelAntennaMap = new ushort[][] {

new ushort[] {

((ushort)(0)),

((ushort)(1))},

new ushort[] {

((ushort)(5)),

((ushort)(6))},

new ushort[] {

((ushort)(10)),

((ushort)(11))},

new ushort[] {

((ushort)(15)),

((ushort)(16))},

new ushort[] {

((ushort)(20)),

((ushort)(21))}};

}

}

You can get more information about CodeDom from

http://msdn.microsoft.com/en-us/library/system.codedom.aspx.

This is a sample:
http://msdn.microsoft.com/en-us/library/h82xde1t(VS.80).aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 03, 2009 6:28 AM
Thanks! I will have to implement your "Array initialization" section in a derived CodeDomSerizalizer
FM1965  Wednesday, July 08, 2009 8:47 PM

Hi FM1965,

Please go through this link to get more information about CodeDomSerializer:
http://msdn.microsoft.com/en-us/library/system.componentmodel.design.serialization.codedomserializer.aspx.

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 09, 2009 2:47 AM

You can use google to search for other answers

Custom Search

More Threads

• Inherited Extender Property Defaults
• Inherited Toolstrips cannot be Designed
• Form.designer separated from its form file
• I cannot get designer view....
• Form Designer problem with Inheritance Events?
• Custom Contro Icon
• Adding EDIT function to DATAGRID Rows
• Adding custom designerverb (combobox)
• Can I use UserControl as a TreeNode?
• Error in Updating database table using OdbcDataAdapter.Update() method