Is there a workaround for value types that make general inference impossible?

Using the method described below:

public class Foo<T1>
{
    public void Bar<T2>(IQux<IBaz<T1, T2>> baz) { }
}

public interface IBaz<T1, T2> { }

public struct Baz : IBaz<SomeType, AnotherType> { }

public interface IQux<out T> {}

public class Qux<T> : IQux<T> {}

Apparently, it is impossible to have Baroutput T2as AnotherTypein a call like this:

new Foo<SomeType>().Bar(new Qux<Baz>());

This will work if Baznot a structure. Is there a workaround when I can save the transfer Bazby value, but also be able to infer the type in such a scenario?

+4
source share
2 answers

This is not a typical type inference problem, but a general error. Barcannot deduce T2how AnotherType, because this is not true.

new Foo<SomeType>().Bar<AnotherType>(new Qux<Baz>());

, Qux<Baz> IQux<IBaz<SomeType,AnotherType>>, Bar. IQux<Baz> IQux<IBaz<SomeType,AnotherType>>, .

, Bar:

public class Foo<T1> {
    public void Bar<T2,TBaz>(IQux<TBaz> baz) where TBaz:IBaz<T1,T2> { }
}

:

new Foo<SomeType>().Bar<AnotherType,Baz>(new Qux<Baz>());

.

+3

PetSerAl. - , , , , , , . .

, , , , , , IBaz<out T>, Baz a class.

a struct, . , class ( ). , , ( PerSetAl).

+1

All Articles