3

Hopefully a simple question

I need to serialise an inherted class, in this example iProperty inherits from 'PropertyDescriptor'. In order to achieve this iProperty needs a parameter less constructor, and the base class has no such constructor, If I attempt to add a construtor I am told the base class does not contain a method thats takes 0 arguments which is correct. How do I get round this issue.

Thanks.

Example

   public class iProperty : PropertyDescriptor
    {
        private string propName;
        private object propValue;

        // Need Parameterless Construtor ?

        public iProperty(string pName, object pValue)
            : base(pName, new Attribute[] { })
        {
            propName = pName;
            propValue = pValue;

        }
    }
5
  • I think we need to see the PropertyDescriptor class (or at least its constructor). Commented Jul 31, 2015 at 15:33
  • You may have to make a serializable wrapper for iProperty, with a method GetiProperty or equivalent that creates the iProperty object based on the serialized info. There's no working around the constructor requirement. Commented Jul 31, 2015 at 15:35
  • 1
    Please show your iProperty parameterless constructor. This could call one of the existing PropertyDescriptor constructors with default values. Commented Jul 31, 2015 at 15:37
  • So if I was to make iProperty an external class so it could be serialised how would I integrate that class into the above code? Thanks Commented Jul 31, 2015 at 15:53
  • You can use Composition instead of Inheritance and mark PropertyDescriptor field with attribute '[XmlIgnore]'. Or change PropertyDescriptor to be an interface. Or create a parameterless constructor for PropertyDescriptor. It is really depends on your architecture and restrictions. Commented Aug 4, 2015 at 15:17

2 Answers 2

7
public class iProperty : PropertyDescriptor
{
    private string propName;
    private object propValue;

    // Need Parameterless Construtor ?
    public iProperty()
        : base("placeholder", new Attribute[] { })
    {
    }

    public iProperty(string pName, object pValue)
        : base(pName, new Attribute[] { })
    {
        propName = pName;
        propValue = pValue;

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, already tried this , it reports 'There was an error reflecting type'
3

You can create a parameterless contructor, but you have to call one of base class contructors anyway

public class iProperty : PropertyDescriptor
{

    public iProperty()
        : base(someDefaults)
    {
    }
}

You can also implement a parameterless constructor in the base class and make it protected. That way no-one except the derived class can call it.

3 Comments

Does not work as it causes exception on serialisation - 'Error reflecting type'
How can I add a constructor to the base class which is part of dotnet?
I tought that PropertyDescription is your class. If it is dotnets, then you cant add a constructor.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.