2

I have an application with lots of generics and IoC. I have an interface like this:

public interface IRepository<TType, TKeyType> : IRepo

Then I have a bunch of tests for my different implementations of IRepository. Many of the objects have dependencies on other objects so for the purpose of testing I want to just grab one that is valid. I can define a separate method for each of them:

public static EmailType GetEmailType()
{
  return ContainerManager.Container.Resolve<IEmailTypeRepository>().GetList().FirstOrDefault();
}

But I want to make this generic so it can by used to get any object from the repository it works with. I defined this:

public static R GetItem<T, R>() where T : IRepository<R, int>
{
  return ContainerManager.Container.Resolve<T>().GetList().FirstOrDefault();
}

This works fine for the implementations that use an integer for the key. But I also have repositories that use string. So, I do this now:

public static R GetItem<T, R, W>() where T : IRepository<R, W>

This works fine. But I'd like to restrict 'W' to either int or string. Is there a way to do that?

The shortest question is, can I constrain a generic parameter to one of multiple types?

3
  • Why do you need to restrict W? Won't this already be implicitly restricted by the fact that you don't have an IRepository implementation with a W that is not int or string? Commented May 20, 2010 at 14:56
  • Ouch, good call. Too bad I can't accept a comment as an answer :P Commented May 20, 2010 at 15:17
  • Well, your actual question was whether you can constrain a generic parameter to one of multiple types and that's still a valid and interesting question. The fact that you don't need to worry about the answer is just a bonus. :) Commented May 20, 2010 at 18:38

2 Answers 2

3

No; you cannot constrain a generic parameter to one of multiple types. Generic restraints are quite simple constructs, actually.

And I think you may shortly run into the fact that generic constraints don't participate in overload resolution. :(

Sign up to request clarification or add additional context in comments.

Comments

0

The shortest answer is: no.

A longer answer is that you can restrict, as you did, to a common base class or interface. But in the case of int and string, the common base class of int and string is Object, which obviously is not an interesting (nor legal if I remember correctly) constraint.

You can restrict a generic parameter to a class or struct, but again, it does not suit your needs.

I guess you are limited to checking at run-time if the type is legal.

Comments

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.