CodeDOM sealed class with virtual properties

D

Dan Holmes

I have code that generates a class.

[Serializable()]
public sealed partial class ProductInfo {

private string _ProductID;
....
public virtual string ProductID {
get {
return _ProductID;
}
set {
_ProductID = value;
}
}
....
}

i can't figure out how to get the virtual off the properties. It reads
table definitions from the DB and creates the class that looks like it.

Here is the code.

CodeTypeDeclaration c = new CodeTypeDeclaration(t.Name + "Info");
c.CustomAttributes.Add(new CodeAttributeDeclaration("Serializable"));
c.IsClass = true;
c.IsPartial = true;
c.TypeAttributes = TypeAttributes.Serializable | TypeAttributes.Sealed |
TypeAttributes.Public;
foreach (Column cn in t.Columns)
{
Type ct = null;
if (cn.DataType.SqlDataType.ToString().Contains("Char") ||
cn.DataType.ToString().Contains("Text"))
ct = typeof(string);
else
ct = Type.GetType("System." + cn.DataType.SqlDataType.ToString());
CodeMemberField f = new CodeMemberField(ct, "_" + cn.Name);
f.Attributes = MemberAttributes.Private ;
c.Members.Add(f);

CodeMemberProperty p = new CodeMemberProperty();
p.Attributes = MemberAttributes.Public;
p.Name = cn.Name;
p.Type = new CodeTypeReference(ct);
p.HasGet = true;
p.GetStatements.Add(new CodeSnippetExpression("return _" + cn.Name));
p.HasSet = true;
p.SetStatements.Add(new CodeSnippetExpression("_" + cn.Name + " =
value"));
c.Members.Add(p);
}

What am i doing wrong?

dan
 
N

Nicholas Paldino [.NET/C# MVP]

Dan,

From the documentation for the MemberAttributes enumeration:
There is no Virtual member attribute. A member is declared virtual by
setting its member access to Public (property1.Attributes =
MemberAttributes.Public) without specifying it as Final. The absence of the
Final flag makes a member virtual in C# (public virtual), overrideable in
Visual Basic (Public Overrideable). To avoid declaring the member as virtual
or overrideable, set both the Public and Final flags in the Attributes
property. See the Attributes property for more information on setting member
attributes.
 

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