Is this possible in C # (I use .Net 4.5, but ask in a more general case) to determine if two implementations of a generic type are functionally equivalent?
As an example of a need, suppose I have an IMapper interface defined as:
public interface IMapper<TTo, TFrom> { TTo MapFrom(TFrom obj); TFrom MapFrom(TTo obj); }
My ideal implementation:
public class MyMapper : IMapper <A, B> { A MapFrom(B obj) {...} B MapFrom(A obj) {...} }
This will be functionally equivalent to:
public class MyEquivalentMapper : IMapper <B, A> { B MapFrom(A obj) {...} A MapFrom(B obj) {...} }
but the compiler (rightfully) recognizes them as different types. Is there a way in which the compiler can treat these two types as equivalent (and perhaps even interchangeably)?
I also looked at this:
public interface ISingleMapper<out TTo, in TFrom> { TTo MapFrom(TFrom obj); } public class MyAlternateMapper : ISingleMapper<A, B>, ISingleMapper<B, A> { A MapFrom(B obj) {...} B MapFrom(A obj) {...} }
but I found that I can not correctly identify the abstraction, so that I can introduce (in the constructors, etc.) a specific class without creating an "average person" interface:
public interface IBidirectionalMapper<TTo, TFrom> : ISingleMapper<TTo, TFrom>, ISingleMapper<TFrom, TTo> { TTo MapFrom(TFrom obj); TFrom MapFrom(TTo obj); } public class MyAlternateMapper : IBidirectionalMapper<A, B> { A MapFrom(B obj) {...} B MapFrom(A obj) {...} }
I think the “average person” approach is more “correct”, but I would prefer not to create an extra type. Furthermore, it still carries a problem when replacing type arguments creates two different, but functionally equivalent types.
Is there a better way to achieve my goal?