2

I have a class like this:

public class Proxy<TClient>() where TClient : ClientBase<TChannel> { } 

I want to be able to specify something like this:

where TClient : ClientBase<TChannel> where TChannel : class 

but without specifying it in the class definition like so:

public class Proxy<TClient, TChannel>() 

Is there a way to do this or am I required to have the second type definition as above?

7
  • Where did TChannel come from? Commented Jun 19, 2013 at 12:27
  • 2
    No, you cannot do that, and yes, you need to specify the second generic parameter in your Proxy declaration: Proxy<TClient, TChannel>. Commented Jun 19, 2013 at 12:28
  • @Dan TChannel is required for ClientBase (which comes from System.ServiceModel). There is no non-generic alternative. Commented Jun 19, 2013 at 12:31
  • I understand that, but in your code, it's not defined anywhere. You have to declare it as a generic type parameter, which is what you're trying to avoid. What you ask for cannot be done. Commented Jun 19, 2013 at 12:33
  • 2
    What do you do with TClient in your class? You could potentially omit TClient and just declare members using ClientBase<TChannel>, depending on usage. Commented Jun 19, 2013 at 12:36

1 Answer 1

2

That's not possible. You have to include TChannel as a generic type parameter of Proxy.

One of the options to get over this “limitation” (in quotes because it is a by-design feature that arises from how the C# type system works) is to use an interface which each channel would be supposed to implement:

public interface IChannel { … } public class Proxy<TClient>() where TClient : ClientBase<IChannel> { } public class MyObscureChannel : IChannel { … } public class MyObscureClient : ChannelBase<MyObscureChannel> { … } … var client = new Proxy<MyObscureClient>(…); // MyObscureChannel is implied here 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.