Alfons
Alfons

Reputation: 531

C# factory construction not working

I am obviously doing something wrong here, but the question is how do i get it working? I got the following code

Note: ParameterEventStreamRef inherits from Parameter, same goes for the StStvariants.

public void DoStuff(Parameter[] parameters)
{
    var Parameters = parameters.Select(x => parameterConverterFactory.GetParameterConverter(x).ToJsonClass(x)).ToArray();
}

public class ParameterConverterFactory: IParameterConverterFactory
{
    public IStackStateJsonConverter<StStParameter, Parameter> GetParameterConverter(StStParameter arg)
    {
        if (arg.GetType() == typeof(StStParameterEventStreamRef))
            return new JsonParameterEventStreamRefConverter();
        throw new InvalidEnumArgumentException(arg.GetType().FullName);
    }

public class JsonParameterEventStreamRefConverter : JsonParameterConverter, IStackStateJsonConverter<StStParameterEventStreamRef, ParameterEventStreamRef>
{

    public ParameterEventStreamRef ToJsonClass(StStParameterEventStreamRef arg, ParameterEventStreamRef source = null)
    {
        if (source == null)
            source = new ParameterEventStreamRef();
        base.ToJsonClass(arg, source);
        source.var1 = arg.var1;
        source.var2 = arg.var2;
        return source;
    }
}

public class JsonParameterConverter : JsonNodeConverter, IStackStateJsonConverter<StStParameter, Parameter>
{
    public Parameter ToJsonClass(StStParameter arg, Parameter source = null)
    {
        if (source == null)
            source = new Parameter();
        source.var0 = arg.var0;

        return source;
    }
}

Problem is: the factory is returning the correct Converter, but when the converter is called, it is "Skipping" the JsonParameterEventStreamRefConverter and going directly to the JsonParameterConverter. I think the reason for this is a part of the logic is done compile-time, the other run-time.

Upvotes: 1

Views: 94

Answers (2)

Quido
Quido

Reputation: 659

In DoStuff() you're calling the ToJsonClass() method. Even though that you 'x' could be any descendant from Parameter, the runtime will not know which one it is. It will look for the ToJsonClass() signature that best matches the signature: ToJsonClass(Parameter). The JsonParamterConverter has a method that matches this exactly, so that's the best match.

You could try replacing the ToJsonClass() in JsonParameterStreamRef with something like this:

public override Parameter ToJsonClass(StStParameter arg, Parameter source = null)
{
    if (arg.GetType() == typeof (StStParameterEventStreamRef))
    {
        return ToJsonClass(arg as StStParameterEventStreamRef, source as ParameterEventStreamRef);
    }
    return base.ToJsonClass(arg, source); // or throw an exception if this is an error.
}

Upvotes: 1

Alfons
Alfons

Reputation: 531

The following is a working solution to the problem, but if you can come up with a better solution, please.

    public Parameter GetConverterFactory_AndDo_ToJsonClass(StStParameter stStParameter)
    {
        if (stStParameter.GetType() == typeof (StStParameterEventStreamRef))
            return new JsonParameterEventStreamRefConverter().ToJsonClass(stStParameter as StStParameterEventStreamRef);
        throw new InvalidEnumArgumentException(stStParameter.GetType().FullName);
    }

Upvotes: 0

Related Questions