C# resolving generic type parameter

114 Views Asked by At

I'm trying to figure out if there is any way possible to resolve an interface's generic parameter type's inner generic type (i.e. IList<T> the type of T) while retaining the original concrete class. For example this is what I have now which works but doesn't read well when used.

public interface IFuzzyValue<TValueType>
{
    double Probability { get; }
    TValueType Value { get; }
}

public static class Util
{
    public static IEqualityComparer<TFuzzyType> ValueEquality<TFuzzyType, TValueType>() 
        where TFuzzyType : IFuzzyValue<TValueType>
    {
        return new ValueEqualityComparer<TFuzzyType, TValueType>();
    }
}

Now to create a value equality comparer for a concrete IFuzzyValue class I need to do the following.

class FuzzyString : IFuzzyValue<string>
{
    double Probability { get; }
    string Value { get; }
    ... other properties/methods
}

IEqualityComparer<FuzzyString> comparer = Util.ValueEquality<FuzzyString, string>();

The part I'm trying to address is to avoid having to supply both types to the ValueEquality<FuzzyString, string> method and instead infer the type string from the FuzzyString class itself.

IEqualityComparer<FuzzyString> comparer = Util.ValueEquality<FuzzyString>();

Is there any way this is possible in C#? I can't seem to find anyway around this type system limitation.

0

There are 0 best solutions below