locked
Overriding a .NET Class: XElement

    Question

  • I'm porting an app from Windows 7 to a Windows Store app, and trying to use as much of the existing code as possible.

    One of my classes has a Property of type XElement. When I try to serialize the class, I get an error saying "No Parameterless Constructor."

    As part of the debugging and understanding what's going on, I thought I would just create my own XElementFoo class, inheriting from XElement, and add a parameterless constructor.

    When I compile XElementFoo, I get an error that "XElement does not contain a constructor that takes 0 arguments." So much for object oriented 101.

    Is there a way around this? I need for XElement or a child of XElement to have a parameterless constructor.

    public class XElementFoo : XElement
    {
        public XElementFoo() { }
    
        public XElementFoo(XElement xElement) : base(xElement) { }
    
        public XElementFoo(XName xName) : base(xName) { }
    
        public XElementFoo(XStreamingElement xStreamingElement) : base(xStreamingElement) { }
    
        public XElementFoo(XName xName, Object obj) : base(xName, obj) { }
    
        public XElementFoo(XName xName, Object[] obj) : base(xName, obj) { }
    }


    Randy

    Thursday, February 20, 2014 7:07 PM

Answers

  • XElement does not have a parameterless construction which means that your class' parameterless constructor cannot instantiate the base class as-is... you need to redirect to one of the other XElement constructors and supply some appropriate data, ex:

            public XElementFoo() : base(XName.Get("XElementFoo")) { }


    -Eric.

    Friday, February 21, 2014 10:17 PM
    Moderator

All replies

  • Can you not modify XElement to have a parameterless constructor?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 8:39 PM
    Moderator
  • I included the code in my original post so anyone interested could just copy/paste and try it. When I put the code above in Visual Studio, it won't compile. If I comment out the parameterless constructor, it compiles and works fine.

    Any ideas why I cannot add a parameterless constructor?


    Randy

    Friday, February 21, 2014 8:43 PM
  • XElement does not have a parameterless construction which means that your class' parameterless constructor cannot instantiate the base class as-is... you need to redirect to one of the other XElement constructors and supply some appropriate data, ex:

            public XElementFoo() : base(XName.Get("XElementFoo")) { }


    -Eric.

    Friday, February 21, 2014 10:17 PM
    Moderator