14

I have created this method which is an object factory:

public static T GetService<T>(T serviceInterface)
{
    if (serviceInterface.Equals(typeof(IMemberService)))
    {
        return (T)(object)new MemberService();
    }
    else if (serviceInterface.Equals(typeof(ILookupService)))
    {
        return (T)(object)new LookupService();
    }
    throw new ArgumentOutOfRangeException("No action is defined for service interface " + serviceInterface.Name);
}

Now, I would like to go further and eliminate the need for "serviceInterface" parameter, but my problem is - I don't know how to compare type parameter T to an interface: doing

T.Equals(typeof(ILookupService)) 

gives compiler error: 'T' is a 'type parameter', which is not valid in the given context.

Any ideas how could I compare a type parameter to an interface?

Thank you, Andrey

1

4 Answers 4

17

You can use typeof(T) to get a Type object back which could replace the use of serviceInterface

For example

public static T GetService<T>()
{
    Type serviceInterface = typeof(T);
    if (serviceInterface.Equals(typeof(IMemberService)))
    {
        return (T)(object)new MemberService();
    }
    else if (serviceInterface.Equals(typeof(ILookupService)))
    {
        return (T)(object)new LookupService();
    }
    throw new ArgumentOutOfRangeException("No action is defined for service interface " + serviceInterface.Name);
}
Sign up to request clarification or add additional context in comments.

10 Comments

Damn, now I'm embarrassed! Soooo simple! Thanks a bunch Jared!
Sure, but will it ever equal an interface? If T represents the type of a concrete object, surely it won't equal an interface.
OK agreed, but it gets smellier by the second
To me it seems like a misappropriation of generics to start your generic method with checks for particular types/interfaces
Andrey, there's probably nothing wrong. I just find it an unusual use of generics when the method branches according to the supplied generic type. In this situation I'd simply create two non-generic methods of fixed type. I appreciate (from Jared's comments) that this may not suit your purposes.
|
2

Use typeof(T).

So,

typeof(T).Equals(typeof(ILookupService))

Comments

1

Could the is operator be applicable here?

1 Comment

is applies to objects not classes, so no, it wouldn't provide the OP with a solution. It could maybe be used in his original design, but not when removing the parameter.
1
if (typeof(IMemberService).IsAssignableFrom(typeof(T)))
{}
else if (typeof(ILookupService).IsAssignableFrom(typeof(T)))
{}

2 Comments

Combine this answer (IsAssignableFrom) with JaredPar's answer, and you'll have the complete solution.
What's wrong with my original solution? I need to know if it the type passed matches exactly to the interface I compare to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.