Andres
Andres

Reputation: 2899

Setting object in C# class constructor using generics

I have a solution where I´m saving different kinds of objects serialized in the property SerializedEvent a common class named Event:

public class Event
{
    public Serializer Serializer = new Serializer();

    /// <summary>
    /// Identificador.
    /// </summary>
    public int ID { get; set; }

    /// <summary>
    /// Identificador del tipo de evento.
    /// </summary>
    public int EventTypeID { get; set; }

    /// <summary>
    /// Identificador de la interfaz.
    /// </summary>
    public int InterfaceID { get; set; }

    /// <summary>
    /// Evento serializado.
    /// </summary>
    private string SerializedEvent { get; set; }
}

Then I have the different classes that serialized, populates that serializedevent property.

What I want, is to have a constructor in each of this specialized class that takes an Event class object as a parameter to do the automatic conversion:

public class ProcessVoucherEvent : BaseEvent<ProcessVoucherEvent>, IEvent
{
    public ProcessVoucherEvent(Event incomingEvent)
    {
        this = ConvertToSpecializedEvent(incomingEvent);
    }
}

Where BaseEvent:

public abstract class BaseEvent<T> where T : IEvent
{
    /// <summary>
    /// Serializador.
    /// </summary>
    private Serializer Serializer = new Serializer();

    /// <summary>
    /// Convierte a un evento generico a un evento especializado.
    /// </summary>
    /// <param name="incomingEvent">Evento a convertir.</param>
    /// <returns>Evento convertido.</returns>
    public T ConvertToSpecializedEvent(Event incomingEvent)
    {
        return (T)Serializer.Deserialize(incomingEvent.GetSerializedEvent(), typeof(T));
    }
}

The problem, is that "this" is a read-only property. So, what can I do to assign the deserialized object of the Event class object to the new object to the ProcessVoucherEvent object?

Do I have to use reflection to set all the properties using for example a foreach? I´m going to have several specialized event classes, and thats the reason why I want to have a generic solution like this.

Any guidance will be appreciated.

The final result I want to get is some code like this:

Event event = GetNextEvent();
var specializedEvent = new ProcessVoucherEvent(event)

Upvotes: 0

Views: 85

Answers (2)

Diego Gomez
Diego Gomez

Reputation: 78

I'm pretty sure that instead of using a constructor to change "this", you should use a Factory pattern to create objects of any class with the serialization in the way that you need. So basically, a factory will provide you a way to create objects of any type you need, just deserialiing your xml

Upvotes: 0

Peter T. LaComb Jr.
Peter T. LaComb Jr.

Reputation: 2975

You can't really do what you're asking in a constructor without resorting to setting properties via reflection or similar. That's not ideal.

What you probably want to do is use a static method or class as a factory.

That factory can return an appropriate T based on the serialized data passed in. Essentially, your constructor becomes a T (IEvent) returning static method, probably on the base class.

Upvotes: 4

Related Questions