Compilation error when overriding a generic type method

AdamStone

I expose the following class in assembly A:

public abstract class ServiceDependencyHost
{
    protected virtual T ReferenceService<T>() where T : ServiceBase
    {
        // Virtual implementation here...
    }
}

I expose this derived class in a separate assembly (B):

public sealed class ProcessServiceOperation : ServiceDependencyHost
{
    public override T ReferenceService<T>()
    {
        // Override implementation here...

        return base.ReferenceService<T>();
    }
}

Using the code as shown above, the compiler complains on this line:

return base.ReferenceService<T>();

The type 'T' cannot be used as type parameter 'T' in the generic type or method A.ReferenceService(). There is no boxing conversion or type parameter conversion from 'T' to 'System.ServiceProcess.ServiceBase'.

Naturally, I tried replicating the constraints in assembly B:

public override T ReferenceService<T>() where T : ServiceBase

But the compiler now warns on the line above...

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

This answer indicates that my solution should have worked. I want to avoid using reflection to expose this method publicly. It should be so simple!

Thanks in advance to anyone who can spot what mistake I am making.

Douglas

The issue is not strictly due to generics. The cause of the issue is that the base class method is protected, whilst the derived class method is public.

An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

Consequently, the compiler assumes that the two methods are distinct, and the derived method fails to inherit the where T : ServiceBase generic constraint from the base method. Since the derived method knows nothing about T, it expectedly complains that T cannot be converted to ServiceBase when you attempt to pass it to the base method.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Compilation error when overriding a generic method with a type parameter

From Dev

Compilation error when overriding a generic method with a type parameter

From Dev

Overriding method with generic return type with no warning

From Dev

error overriding method of class that extends generic class

From Dev

Name clash when overriding method of generic class

From Dev

Compilation error: Type List can't be Generic

From Dev

Compilation error: Type List can't be Generic

From Dev

Overriding method with a lower-visibility one results in compilation error

From Dev

Cannot change return type when overriding method, error when using generics

From Dev

Cannot change return type when overriding method, error when using generics

From Dev

Overriding necessary to avoid runtime type check for Java generic method?

From Dev

Overriding a method with a generic return type fails after adding a parameter

From Dev

Incompatible return type when overriding methods of Generic classes in Java

From Dev

Overriding abstract generic method in Java

From Dev

Java Generic Class Method Overriding

From Dev

Overriding the method of a generic trait in Scala

From Dev

Java Generic Class Method Overriding

From Dev

Why calling a generic method with different types gives compilation error?

From Dev

Overriding non-generic methods by generic method

From Dev

Why subsignature and unchecked rules work this way on return types when overriding a generic method with a non-generic one?

From Dev

Why does overriding this generic method only work if the base method includes an unused type parameter?

From Dev

Generic method triggers type safety error - why?

From Dev

Cannot implicitly convert type error in generic method

From Dev

When overriding a method

From Dev

Method of parameterised Type must not use local symbol error when making generic interface factory

From Dev

Generic Method Won't Recognise Generic Type When Passing It as a Parameter

From Dev

return generic IEnumerable when passing concrete type parameter for a generic method

From Dev

Use PropertyInfo as generic type when calling a generic method

From Dev

Error when returning interface with generic type

Related Related

  1. 1

    Compilation error when overriding a generic method with a type parameter

  2. 2

    Compilation error when overriding a generic method with a type parameter

  3. 3

    Overriding method with generic return type with no warning

  4. 4

    error overriding method of class that extends generic class

  5. 5

    Name clash when overriding method of generic class

  6. 6

    Compilation error: Type List can't be Generic

  7. 7

    Compilation error: Type List can't be Generic

  8. 8

    Overriding method with a lower-visibility one results in compilation error

  9. 9

    Cannot change return type when overriding method, error when using generics

  10. 10

    Cannot change return type when overriding method, error when using generics

  11. 11

    Overriding necessary to avoid runtime type check for Java generic method?

  12. 12

    Overriding a method with a generic return type fails after adding a parameter

  13. 13

    Incompatible return type when overriding methods of Generic classes in Java

  14. 14

    Overriding abstract generic method in Java

  15. 15

    Java Generic Class Method Overriding

  16. 16

    Overriding the method of a generic trait in Scala

  17. 17

    Java Generic Class Method Overriding

  18. 18

    Why calling a generic method with different types gives compilation error?

  19. 19

    Overriding non-generic methods by generic method

  20. 20

    Why subsignature and unchecked rules work this way on return types when overriding a generic method with a non-generic one?

  21. 21

    Why does overriding this generic method only work if the base method includes an unused type parameter?

  22. 22

    Generic method triggers type safety error - why?

  23. 23

    Cannot implicitly convert type error in generic method

  24. 24

    When overriding a method

  25. 25

    Method of parameterised Type must not use local symbol error when making generic interface factory

  26. 26

    Generic Method Won't Recognise Generic Type When Passing It as a Parameter

  27. 27

    return generic IEnumerable when passing concrete type parameter for a generic method

  28. 28

    Use PropertyInfo as generic type when calling a generic method

  29. 29

    Error when returning interface with generic type

HotTag

Archive