Michele mpp Marostica
Michele mpp Marostica

Reputation: 2472

How to serialize to XML generic classes?

I have these interfaces:

public interface IParameter
{
    string Name { get; }
    object UntypedValue { get; set; }
}
public interface IValidationPolicy<T>
{
    bool Validate(T toValidate);
    T Default();
}

A parameter base class

[Serializable]
public abstract class ParameterBase : IParameter
{
    public abstract string Name { get; protected set; }
    public abstract object UntypedValue { get; set; }
}

A parameter concrete class (I have more but them are quite similar):

public class Parameter<T, V> : ParameterBase where V : IValidationPolicy<T>
{
    [XmlAttribute("Name")]
    public override string Name { get; protected set; }

    [XmlIgnore]
    protected V validation_policy_;
    [XmlElement("AnyValidation", Type = typeof(AnyValidation<>))]
    [XmlElement("MultiOptionsValidation", Type = typeof(MultiOptionsValidation<>))]
    [XmlElement("RangeValidation", Type = typeof(RangeValidation<>))]
    [XmlElement("TextValidation", Type = typeof(TextValidation))]
    public V Validation
    {
        get
        {
            return validation_policy_;
        }
    }

    [XmlIgnore]
    protected T value_;
    [XmlElement("Value")]
    public T Value
    {
        get
        {
            return value_;
        }
        set
        {
            if (validation_policy_.Validate(value))
            {
                value_ = value;
            }
        }
    }

    [XmlIgnore]
    public object UntypedValue
    {
        get
        {
            return Value;
        }
        set
        {
            throw new NotImplementedException();
        }
    }
}

And an XMLParameter class:

public class XMLParameter : INotifyPropertyChanged
{        
    public string Description { get; set; }
    public int PasswordLevel { get; set; }
    public bool Enabled { get; set; }
    public ParameterBase Parameter { get; set; }
}

How can I serialize and deserialize a list of XMLParameters?

In particular I have problem on serializing the IParameter objects.

Since the interface is not serializable as first attempt I created a base abstract class ParameterBase and derive the Parameter from it. But when I try to serialize it in a test method:

var validation = new RangeValidation<int>() { MinValue = 1, MaxValue = 6 };
var parameter = new Parameter<int, RangeValidation<int>>();
parameter.Initialize("NumberOfTrays", validation);
parameter.Value = 6;

XElement par = validation.ToXElement<Parameter<int, RangeValidation<int>>>();

I got an exception: Error at reflection of type 'ConfigurableLibray.Parameter'2[System.Int32,ConfigurableLibray.RangeValidation'1[System.Int32]]'

The inner exception says that ConfigurableLibray.Parameter'2[T,V] is not supported

What am I doing wrong?

Thanks in advance for any suggestion!

Upvotes: 0

Views: 346

Answers (1)

Michele mpp Marostica
Michele mpp Marostica

Reputation: 2472

I solved implementing manually the serialization and deserialization of the classes using reflection.

Upvotes: 0

Related Questions