Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 does not compile when generic type matches overloaded method that takes that type

My project compiles in VS 2013 but does not compile in VS 2015. Below code reproduces the compile problem. The Validator classes are actually in a 3rd party assembly so I can not change the implementation. The require class is a local class but I don't want to change the implementation because I will have to change lots of validation logic. Below is the code that does not compile in VS 2015.

public abstract class Validator<T> : Validator
{
    public override void DoValidate(object objectToValidate)
    {

    }
    protected abstract void DoValidate(T objectToValidate);
}

public abstract class Validator
{
    public abstract void DoValidate(object objectToValidate);
}

public abstract class ValidatorBase<T> : Validator<T>
{
    protected override void DoValidate(T objectToValidate)
    {

    }
}

public class Required : ValidatorBase<object>
{

}

Is there a workaround for this compilation issue? Any help would be appreciated.

The Error:

Severity  Code    Description                                                                                     Project  File        Line

Error     CS0534  'Required' does not implement inherited abstract member 'Validator<object>.DoValidate(object)'           Program.cs  38
like image 692
user2101274 Avatar asked Oct 16 '15 18:10

user2101274


1 Answers

I tried to find a reason of this behavior, I failed.

I did find a workaround though. The code you posted compiles when ValidatorBase<T> is not abstract. I know you can't change it, but you can add another non-abstract class to the inheritance chain:

public class Workaround<T> : ValidatorBase<T> { }

public class Required : Workaround<object>
{

}

It looks like Roslyn doesn't resolve the overridden methods in abstract classes until a non-abstract derived class is defined.

like image 98
Jakub Lortz Avatar answered Nov 12 '22 19:11

Jakub Lortz