none
how to decorate linq2sql generated classes to customize serialization? RRS feed

  • Question

  • Hi, I'm using L2ST4 - LINQ to SQL templates for T4 to generate classes to be used with linq2sql.

    I'd like to decorate my classes and customize how they are serialized, for example in some classes I0d like to set some properties to XmlIgnore, some to XmlElement, XmlAttribute etc. and change also their name. My final goal is to deserialize an XML to my objects, and insert them in my DB using LINQ.

    I can modify directly the generated.cs file but I think it's not a good approach, as if I need to run the custom tool again it will overwrite it and all my modifications will be gone.

    I've tried to create partial classes, and to override the properties with a derivated class, as suggested here, in my case I I'm trying to decorate the class called "PublicationAuthor", here a snippet:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    using System.ComponentModel.DataAnnotations;
    
    namespace MyClasses.Data
    {
        [XmlInclude(typeof(DerivedClass))]
        partial class PublicationAuthor
        {
        }
    
        public class DerivedClass : PublicationAuthor
        {
            public DerivedClass() { }
    
            [XmlIgnore]
            public override int id { get; set; }
        }
    }
    

     

    but I get this error when I compile my classlibrary:

    Error 1 'MyClasses.Data.DerivedClass.id.get': cannot override inherited member 'MyClasses.Data.MyObject.id.get' because it is not marked virtual, abstract, or override. Same error for "set".

    Any ideas on how to accomplish my goal? Modify the T4 template is not an option as I have many classes and I need different customizations to be done manually...

    Thanks for letting me know, cheers



    • Edited by firepol Thursday, September 8, 2011 9:28 AM
    Thursday, September 8, 2011 9:19 AM

Answers

  • Well,

    At first sight it looks like it should works.

    I have a set of serializable objects without ineritance and it is working ok. If it can help you, here you have my sample code:

        [Serializable]
        [KnownType(typeof(cam))]
        [KnownType(typeof(lca))]
        public class acc
        {
            [XmlAttribute()]
            public string c1 { get; set; }
    
            [XmlAttribute()]
            public string c2 { get; set; }
    
            [XmlAttribute()]
            public string c3 { get; set; }
    
            [XmlAttribute()]
            public string c4 { get; set; }
    
            [XmlElement("cam")] 
            public List<cam> cam { get; set; }
    
            public lca lca { get; set; }
            public string ToXml()
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(acc));
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true }))
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    xmlSerializer.Serialize(writer, this, ns);
                    return sb.ToString();
                }
            }
        }
    
        [Serializable]
        public class cam
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlAttribute()]
            public string i { get; set; }
            
            [XmlAttribute()]
            public string f { get; set; }
        }
    
        [Serializable]
        [KnownType(typeof(reg))]
        public class lca
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlElement("reg")] 
            public List<reg> reg { get; set; }
        }
    
        [Serializable]
        [KnownType(typeof(cam))]
        public class reg
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlElement("cam")] 
            public List<cam> cam { get; set; }
        }
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by Larcolais Gong Friday, September 16, 2011 2:56 AM
    Thursday, September 8, 2011 2:28 PM

All replies

  • Hi,

    Why Id property is defined with override statement? Has PublicationAuthor, an interface, or some other parent class an Id property?

    If not, you should remove the override as follows:

    namespace MyClasses.Data
    {
        [XmlInclude(typeof(DerivedClass))]
        partial class PublicationAuthor
        {
        }
    
        public class DerivedClass : PublicationAuthor
        {
            public DerivedClass() { }
    
            [XmlIgnore]
            public int id { get; set; }
        }
    }
    
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Thursday, September 8, 2011 9:43 AM
  • Ok I can do what you say, but... if I serialize my main object, it doesn't take in considration my decorations inside the DerivedClass. The only way is to serialize the DerivedClass.

    Also the problem is my main goal, in the end I want to DEserialize the XML and create the objects to be user by LINQ. And LINQ won't insert in the DB the DerivedClass...

    Thursday, September 8, 2011 10:29 AM
  • Well,

    I cant test your case at the moment, byt try with something like this:

    namespace MyClasses.Data
    {
        [Serializable]
        [KnownType(typeof(DerivedClass))]
        [XmlInclude(typeof(DerivedClass))]
        partial class PublicationAuthor
        {
        }
    
        [Serializable]
        [KnownType(typeof(PublicationAuthor))]
        public class DerivedClass : PublicationAuthor
        {
            public DerivedClass() { }
    
            [XmlIgnore]
            public int id { get; set; }
        }
    }
    
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    Thursday, September 8, 2011 10:39 AM
  • Even so, whatever I insert in DefivedClass won't be taken in consideration (id not ignored, another property set as XmlAttribute always showing as element...)
    Thursday, September 8, 2011 2:10 PM
  • Well,

    At first sight it looks like it should works.

    I have a set of serializable objects without ineritance and it is working ok. If it can help you, here you have my sample code:

        [Serializable]
        [KnownType(typeof(cam))]
        [KnownType(typeof(lca))]
        public class acc
        {
            [XmlAttribute()]
            public string c1 { get; set; }
    
            [XmlAttribute()]
            public string c2 { get; set; }
    
            [XmlAttribute()]
            public string c3 { get; set; }
    
            [XmlAttribute()]
            public string c4 { get; set; }
    
            [XmlElement("cam")] 
            public List<cam> cam { get; set; }
    
            public lca lca { get; set; }
            public string ToXml()
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(acc));
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true }))
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add("", "");
                    xmlSerializer.Serialize(writer, this, ns);
                    return sb.ToString();
                }
            }
        }
    
        [Serializable]
        public class cam
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlAttribute()]
            public string i { get; set; }
            
            [XmlAttribute()]
            public string f { get; set; }
        }
    
        [Serializable]
        [KnownType(typeof(reg))]
        public class lca
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlElement("reg")] 
            public List<reg> reg { get; set; }
        }
    
        [Serializable]
        [KnownType(typeof(cam))]
        public class reg
        {
            [XmlAttribute()]
            public string t { get; set; }
    
            [XmlElement("cam")] 
            public List<cam> cam { get; set; }
        }
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by Larcolais Gong Friday, September 16, 2011 2:56 AM
    Thursday, September 8, 2011 2:28 PM