mercredi 21 septembre 2016

C# 'Class' is a type, not valid in the given context when write factory method with generics

I am finding difficult to create a factory method to return the following base class:

public sealed class ClassA : GeneratedMessage<ClassA, ClassA.Builder>{}
public sealed class ClassB : GeneratedMessage<ClassB, ClassB.Builder>

public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
        where TMessage : GeneratedMessage<TMessage, TBuilder>
        where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
        {
        }

public class EventFactory
{

    public GeneratedMessage<TMessage, TBuilder> GetEvent<TMessage, TBuilder>(int typeId)
            where TMessage : GeneratedMessage<TMessage, TBuilder>
            where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
    {
        switch(typeId)
        {
            case 1:
                ClassA
                beak;
            case 2:
                ClassB
        }
    }
}

How to write so that i can return ClassA if 1 and ClassB if 2?

When i use return ClassA or ClassB in factory method, i am getting 'ClassA is a type', which is not valid in the given context.

Thanks

Aucun commentaire:

Enregistrer un commentaire