简体   繁体   中英

implementing a generic interface with type constraints

I have a Visual Studio 2008 C# 2.0 CF project where I am implementing a generic interface, IComparison . The IComparison.Compare method may be called on to do any type of comparison that is valid to for the objects specified, so I don't want to put a type constraint on it.

public interface IComparison<EXPECTED_PARAM>
{
    Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned);
}

The implementation, however, can be more specific. In this case, I'd like to say that the parameter given to GreaterThan.Compare can be compared to the EXPECTED_PARAM given in the constructor via System.IComparable .

public class GreaterThan<EXPECTED_PARAM> : IComparison<EXPECTED_PARAM>
{
    private EXPECTED_PARAM expected_;

    public GreaterThan(EXPECTED_PARAM expected)
    {
        expected_ = expected;
    }

    public Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned) 
        where RETURNED_PARAM : IComparable< EXPECTED_PARAM >
    {
        return ((returned == null && expected_ == null) ||
                (returned != null && returned.CompareTo( expected_ ) > 0)) ?
               Result.Fail : Result.Pass;
    }
}

Unfortunately, this gives me the error:

error CS0460: Constraints for override and explicit interface implementation methods are inherited from the base method, so they cannot be specified directly

What do I need to do to get the ability to perform arbitrary comparisons of EXPECTED_PARAM objects with RETURNED_PARAM objects?

Thanks, PaulH

How about this?

    public interface IComparison<EXPECTED_PARAM, RETURNED_PARAM>
{
    Result Compare(RETURNED_PARAM returned);
}

public class GreaterThan<EXPECTED_PARAM, RETURNED_PARAM> : IComparison<EXPECTED_PARAM, RETURNED_PARAM> where RETURNED_PARAM : IComparable<EXPECTED_PARAM>
{
    private EXPECTED_PARAM expected_;      
    public GreaterThan(EXPECTED_PARAM expected)     
    {         expected_ = expected;     }      

    public Result Compare(RETURNED_PARAM returned)          
    {
        return ((returned == null && expected_ == null) || 
            (returned != null && returned.CompareTo( expected_ ) > 0)) ?                
            Result.Fail : Result.Pass;
    }
} 

Your inheritance hierarchy is the problem. GreaterThan inherits from IComparison<EXPECTED_PARAM> , which means you're telling the compiler that it implements Compare<EXPECTED_PARAM> , but you want it to implement Compare<RETURNED_PARAM> instead. You could drop the generic constraint on your interface:

public interface IComparison
{
    Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned)
    where RETURNED_PARAM : IComparable;
}

public class GreaterThan<EXPECTED_PARAM>: IComparison
{
    private EXPECTED_PARAM expected_;

    public GreaterThan(EXPECTED_PARAM expected)
    {
        expected_ = expected;
    }

    public Result Compare<RETURNED_PARAM>(RETURNED_PARAM returned) 
        where RETURNED_PARAM : IComparable
    {
        return ((returned == null && expected_ == null) ||
                (returned != null && returned.CompareTo( expected_ ) > 0)) ?
               Result.Fail : Result.Pass;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM