Quick CodeDOM Question

G

Guest

Hi,

I am working on CodeDOM to generate a more usable class to serialize
and deserialize WXS files. I have hit the wall at a pretty early
stage. :-( I am attempting to change the member public fields into
properties. I have written a generic field to property code for that.
It is almost straight out of the link that you gave me. :)

But that always creates code with the virtual keyword. I am not sure
on how to get rid of it. For example this is the sample Icon class
created.

public class Icon {
private string _id;
private string _src;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public virtual string Id {
get {
return this._id;
}
set {
this._id = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public virtual string src {
get {
return this._src;
}
set {
this._src = value;
}
}
}

public virtual string Id ...
public virtual string src ...

As you can see the properties above have the ugly 'virtual' keyword. I
am not sure how to get rid of it.
 
R

Richard Blewett [DevelopMentor]

Can you show us a cut down, compilable, version of the code that generates a virtual property

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I am working on CodeDOM to generate a more usable class to serialize
and deserialize WXS files. I have hit the wall at a pretty early
stage. :-( I am attempting to change the member public fields into
properties. I have written a generic field to property code for that.
It is almost straight out of the link that you gave me. :)

But that always creates code with the virtual keyword. I am not sure
on how to get rid of it. For example this is the sample Icon class
created.
 
G

Guest

This is my code

<style type="text/css">.cf { font-family: Courier New; font-size: 10pt;
color: #000000; background: #ffffff; border-top: windowtext 1pt solid;
padding-top: 0pt; border-left: windowtext 1pt solid; padding-left: 0pt;
border-right: windowtext 1pt solid; padding-right: 0pt; border-bottom:
windowtext 1pt solid; padding-bottom: 0pt; }.cl { margin: 0px; }.cb1 { color:
#0000ff; }.cb2 { color: #008000; }</style><div class="cf"><p class="cl"><span
class="cb1">foreach</span>(CodeTypeDeclaration type <span
class="cb1">in</span> ns.Types)</p><p class="cl"> {</p><p
class="cl"> <span class="cb1">if</span>(type.IsClass ||
type.IsStruct)</p><p class="cl"> {</p><p class="cl">
CodeTypeMember[] members=<span class="cb1">new</span>
CodeTypeMember[type.Members.Count];</p><p class="cl">
type.Members.CopyTo(members,0);</p><p class="cl"> <span
class="cb1">foreach</span>(CodeTypeMember member <span class="cb1">in</span>
members)</p><p class="cl"> {</p><p class="cl">
<span class="cb1">if</span>(member <span class="cb1">is</span>
CodeMemberField)</p><p class="cl"> {</p><p
class="cl"> CodeMemberProperty prop=<span
class="cb1">new</span> CodeMemberProperty();</p><p class="cl">
prop.Name=member.Name;</p><p class="cl">
prop.Attributes=MemberAttributes.Public;</p><p class="cl">
prop.Type=((CodeMemberField)member).Type;</p><p class="cl">
prop.HasGet=<span class="cb1">true</span>;</p><p
class="cl"> prop.HasSet=<span
class="cb1">true</span>;</p><p class="cl">
prop.CustomAttributes.AddRange(member.CustomAttributes);</p><p class="cl">
member.CustomAttributes.Clear();</p><p class="cl">
prop.Comments.AddRange(member.Comments);</p><p
class="cl"> member.Comments.Clear();</p><p
class="cl">
member.Attributes=MemberAttributes.Private;</p><p class="cl">
<span class="cb1">char</span>[]
letters=member.Name.ToCharArray();</p><p class="cl">
letters[0]=Char.ToLower(letters[0]);</p><p class="cl">
member.Name=String.Concat("_",<span class="cb1">new</span> <span
class="cb1">string</span>(letters));</p><p class="cl"> </p><p class="cl">
<span class="cb2">//add get and set</span></p><p
class="cl"> CodeFieldReferenceExpression
refReturnStatement=<span class="cb1">new</span>
CodeFieldReferenceExpression(<span class="cb1">new</span>
CodeThisReferenceExpression(),member.Name);</p><p class="cl">
CodeMethodReturnStatement retStatement=<span
class="cb1">new</span> CodeMethodReturnStatement(refReturnStatement);</p><p
class="cl">
prop.GetStatements.Add(retStatement);</p><p class="cl"> </p><p class="cl">
<span class="cb2">//_member=value</span></p><p
class="cl"> CodeArgumentReferenceExpression
refValuePart=<span class="cb1">new</span>
CodeArgumentReferenceExpression("value");</p><p class="cl">
CodeAssignStatement asStatement=<span class="cb1">new</span>
CodeAssignStatement(refReturnStatement,refValuePart);</p><p class="cl">
prop.SetStatements.Add(asStatement);</p><p
class="cl"> type.Members.Add(prop);</p><p
class="cl"> }</p><p class="cl">
}</p><p class="cl"> }</p></div>
 
G

Guest

Sorry about the earlier message. It was rouge HTML. This is a much readable
version of the code. Thanks for all your help.

foreach(CodeTypeDeclaration type in ns.Types)
{
if(type.IsClass || type.IsStruct)
{
CodeTypeMember[] members=new CodeTypeMember[type.Members.Count];
type.Members.CopyTo(members,0);
foreach(CodeTypeMember member in members)
{
if(member is CodeMemberField)
{
CodeMemberProperty prop=new CodeMemberProperty();
prop.Name=member.Name;
prop.Attributes=MemberAttributes.Public;
prop.Type=((CodeMemberField)member).Type;
prop.HasGet=true;
prop.HasSet=true;
prop.CustomAttributes.AddRange(member.CustomAttributes);
member.CustomAttributes.Clear();
prop.Comments.AddRange(member.Comments);
member.Comments.Clear();
member.Attributes=MemberAttributes.Private;
char[] letters=member.Name.ToCharArray();
letters[0]=Char.ToLower(letters[0]);
member.Name=String.Concat("_",new string(letters));
//add get and set
CodeFieldReferenceExpression refReturnStatement=new
CodeFieldReferenceExpression(new CodeThisReferenceExpression(),member.Name);
CodeMethodReturnStatement retStatement=new
CodeMethodReturnStatement(refReturnStatement);
prop.GetStatements.Add(retStatement);
//_member=value
CodeArgumentReferenceExpression refValuePart=new
CodeArgumentReferenceExpression("value");
CodeAssignStatement asStatement=new
CodeAssignStatement(refReturnStatement,refValuePart);
prop.SetStatements.Add(asStatement);
type.Members.Add(prop);
}
}
}
 
R

Richard Blewett [DevelopMentor]

TO save me having to extract the bits from this code snippet that produce the result you see and then create the type form the codeDOM, and so I then don't have to wrap it in a method body and a class just so I can try it out and experiment.

Any chance you could post something short, that *compiles* that shows your issue. I'm sure it will be quicker for you to strip down to the bare essentials your code rather than me try to build it up from what you have provided.

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Sorry about the earlier message. It was rouge HTML. This is a much readable
version of the code. Thanks for all your help.

foreach(CodeTypeDeclaration type in ns.Types)
{
if(type.IsClass || type.IsStruct)
{
CodeTypeMember[] members=new CodeTypeMember[type.Members.Count];
type.Members.CopyTo(members,0);
foreach(CodeTypeMember member in members)
{
if(member is CodeMemberField)
{
CodeMemberProperty prop=new CodeMemberProperty();
prop.Name=member.Name;
prop.Attributes=MemberAttributes.Public;
prop.Type=((CodeMemberField)member).Type;
prop.HasGet=true;
prop.HasSet=true;
prop.CustomAttributes.AddRange(member.CustomAttributes);
member.CustomAttributes.Clear();
prop.Comments.AddRange(member.Comments);
member.Comments.Clear();
member.Attributes=MemberAttributes.Private;
char[] letters=member.Name.ToCharArray();
letters[0]=Char.ToLower(letters[0]);
member.Name=String.Concat("_",new string(letters));
//add get and set
CodeFieldReferenceExpression refReturnStatement=new
CodeFieldReferenceExpression(new CodeThisReferenceExpression(),member.Name);
CodeMethodReturnStatement retStatement=new
CodeMethodReturnStatement(refReturnStatement);
prop.GetStatements.Add(retStatement);
//_member=value
CodeArgumentReferenceExpression refValuePart=new
CodeArgumentReferenceExpression("value");
CodeAssignStatement asStatement=new
CodeAssignStatement(refReturnStatement,refValuePart);
prop.SetStatements.Add(asStatement);
type.Members.Add(prop);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top