generics - How can I make the C# compiler infer these type parameters automatically? -


I have some code that looks like the first I have some domain class and some special comparisons for them.

  Public Class Fruits {Public Intel Calories {Receive; Set; } Public string name {get; Set; }} Public Class Footer Acquisition Compañar: IQuality Comparator & lt; Fert & GT; {// ...} // There is only one group of basket fruit Public Class Basket Aquacity Comparator: iQquality Comparier & lt; EMemerbable and Left Foot & gt; & Gt; {// ...}  

Next, I have a supporting class called constraintchecker . This is a simple base exclusions method which ensures that some simple base matters are considered:

  public static class constraintchecker {public static bull base TLHS, TCLS) {Bool sameObject = l == R; Bool leftNull = l == faucet; Bull right blue = R == zero; The same object & amp; Amp; ! LeftNull & amp; Amp; ! RightNull; }  

There is also a semantic aquals method which is just a base exclusions check and a comparator function that you specify.

Public Stable Bull Semantic Excellence & Lieutenant; T & G; (TLHS, TCR, Funk & TT; T, T, Ball; F) {Return Base Exclusions (LHS, RAS) & amp; F (LHS, RAS); }

and in the end there is a semantic suction ecclesia method which is two IEnumerable & lt; T & gt; To compare , and accept an IEqualityComparer example Enumerable Succession Eclairs .

  Public Static Bull Semantic Sevens Eccles and LT; T, U, V & gt; (U LHS, U RS, V) calls on each pair elements in the list. Comparator) where U: IEnumerable & lt; T & gt; Where V: IEEquality Comparator & lt; T & gt; {Return Cementic Accolve (LHS, RLS, (L, R) => LHS Sevens Equal (RS, comparator)); <}>  

The point of SemanticSequenceEquals is that you do not have to define two comparisons whenever you compare both IEnumerable and LT Want; T & gt; and T examples; Now you can only select IEqualityComparer & lt; T & gt; and when you SemanticSequenceEquals I can get rid of the basketquality compatibility class, which would be nice.

But there is a problem. The C # compiler can not understand the types involved when adding semantic suction Ecloss:

  // error! Compiler can not guess type parameter. ConstraintChecker.SemanticSequenceEquals (LHS, RHS, New FruitEqualityComparer ()) Return;  

If I specify them explicitly, it works:

  Return ConstraintChecker.SemanticSequenceEquals & LT; Fruits, IEnumerable & LT; Fruit & gt ;, IEqualityComparer & LT; Fruit & gt; & Gt; (LHS, RAS, New Foot Aquaculture Immerser ()); Obviously, this is a big problem, and it is not very dry, can I change here, so that I do not have to type type parameters clearly?   

Just try and specify without u TV

  static bool SemanticSequenceEquals. & Lt; T & gt; (IEnumerable & lt; t & gt; LHS, IEnumerable & lt; t & gt; RHS, IEqualityComparer & lt; t & gt; comparator) {SemanticEquals Return (Lhs, rhs, (l, r) = & gt; lhs. SequenceEqual (rhs, comparator)); }  

Comments