Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

CodeDOM sealed class with virtual properties

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Holmes - 23 Oct 2007 15:59 GMT
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
Nicholas Paldino [.NET/C# MVP] - 23 Oct 2007 16:17 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have code that generates a class.
>
[quoted text clipped - 52 lines]
>
> dan

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.