Get takes two type arguments. When you call service. Get(new Signatur()); how does the compiler know what T is?
You'll have to pass it explicitly or change something else about your type hierarchies. Passing it explicitly would look like: service. Get(new Signatur()).
This is the answer. – vulkanino Oct 12 '10 at 17:21 Yes, I wanted to avoid the explicite call. But if as from Eric Lippert explaint, the generic constraints are not used for solving the generic return types, then this would not work.
Thank you! – Ben Oct 13 '10 at 10:19.
Kirk's answer is right on. As a rule, you're not going to have any luck with type inference when your method signature has fewer types of parameters than it has generic type parameters. In your particular case, it seems you could possibly move the T type parameter to the class level and then get type inference on your Get method: class ServiceGate { public IAccess Get(S sig) where S : ISignatur { throw new NotImplementedException(); } } Then the code you posted with the CS0411 error could be rewritten as: static void Main() { // Notice: a bit more cumbersome to write here... ServiceGate service = new ServiceGate(); // ...but at least you get type inference here.
IAccess access = service. Get(new Signatur()); }.
As I mentioned in my comment, I think the reason why this doesn't work is because the compiler can't infer types based on generic constraints. Below is an alternative implementation that will compile. I've revised the IAccess interface to only have the T generic type parameter.
Interface ISignatur { Type Type { get; } } interface IAccess { ISignatur Signature { get; } T Value { get; set; } } class Signatur : ISignatur { public Type Type { get { return typeof(bool); } } } class ServiceGate { public IAccess Get(ISignatur sig) { throw new NotImplementedException(); } } static class Test { static void Main() { ServiceGate service = new ServiceGate(); var access = service. Get(new Signatur()); } }.
Now my aim was to have one pair with an base type and a type definition (Requirement A). For the type definition I want to use inheritance (Requirement B). The use should be possible, without explicite knowledge over the base type (Requirement C).
After I know now that the gernic constraints are not used for solving the generic return type, I experimented a little bit: Ok let's introducte Get2: class ServiceGate { public IAccess Get1(C control) where C : ISignatur { throw new NotImplementedException(); } public IAccess Get2(ISignatur control) { throw new NotImplementedException(); } } class Test { static void Main() { ServiceGate service = new ServiceGate(); //var bla1 = service. Get1(new Signatur()); // CS0411 var bla = service. Get2(new Signatur()); // Works } } Fine, but this solution reaches not requriement B.
Next try: class ServiceGate { public IAccess Get3(C control, ISignatur iControl) where C : ISignatur { throw new NotImplementedException(); } } class Test { static void Main() { ServiceGate service = new ServiceGate(); //var bla1 = service. Get1(new Signatur()); // CS0411 var bla = service. Get2(new Signatur()); // Works var c = new Signatur(); var bla3 = service.
Get3(c, c); // Works! } } Nice! Now the compiler can infer the generic return types.
But I don't like it. Other try: class IC { public IC(A a, B b) { Value1 = a; Value2 = b; } public A Value1 { get; set; } public B Value2 { get; set; } } class Signatur : ISignatur { public string Test { get; set; } public IC Get() { return new IC(this, this); } } class ServiceGate { public IAccess Get4(IC control) where C : ISignatur { throw new NotImplementedException(); } } class Test { static void Main() { ServiceGate service = new ServiceGate(); //var bla1 = service. Get1(new Signatur()); // CS0411 var bla = service.
Get2(new Signatur()); // Works var c = new Signatur(); var bla3 = service. Get3(c, c); // Works! Var bla4 = service.
Get4((new Signatur()).Get()); // Better... } } My final solution is to have something like ISignature, where B ist the base type and C the definition...
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.