none
Interfaces and generics RRS feed

  • Question

  • I need to extend an interface to implement a base interface and have some extra methods/properties

    I want to do this in generic way so I don't have to repeat this code 100+ times.

    I was thinking to do something like this (pseudocode: this won't compile, I know)

    public interface IOverload<TBase> : TBase where TBase : interface { /// <summary> /// Get base object /// </summary> TBase OriginalObject { get; } /// <summary> /// Create/Modify base object /// </summary> void Save(); }

    public class OverloadClass : IOverload<IMyObject>

    // should implement IMyObject + methods from IOverload<IMyObject>

    Where IOverload<IMyObject> is eyecandy for " : IMyObject, IOverload<>"

    // Currently you have to write this

    public interface IOverload<TBase>

    public class OverloadClass : IMyObject, IOverload<IMyObject>

    My question: is there any way to implement this kind of behaviour?



    • Edited by Crispy76 Tuesday, October 6, 2015 8:00 AM
    Tuesday, October 6, 2015 7:28 AM

Answers

  • BTW: I know this isn't possible, just wanted to know if anyone could supply me with a good workaround for this problem. My classes now implement TBase and IOverload<TBase>  directly, which is cluttering my class inheritance structure. (TBase and IOverload<TBase> are not real classes, merely chosen to illustrate the problem)

    Hi Crispy76,

    As you said, it isn't possible. So this is by design, as far as I know, there is no any workaround for this problem. And it is the bottom of developing program language. If you must have a workaround, I would suggest you go to UserVoice site to submit this issue. Thanks for your understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 14, 2015 8:11 AM

All replies

  • A related restriction in Standard ECMA-335 (6th Edition / June 2012) II.9.1:

    Generic type definitions are subject to the following restrictions:

    • A generic parameter, on its own, cannot be used to specify the base class, or any implemented interfaces. So, for example, .class … G`1<T> extends !0 is invalid. However, it is valid for the base class, or interfaces, to use that generic parameter when nested within another generic type. For example, .class … G`1<T> extends class H`1<!0> and .class … G`1<T> extends class B`2<!0,int32> are valid.

      [Rationale: This permits checking that generic types are valid at definition time rather than at instantiation time. e.g., in .class … G`1<T> extends !0, we do not know what methods would override what others because no information is available about the base class; indeed, we do not even know whether T is a class: it might be an array or an interface. Similarly, for .class … C`2<(!1)T,U> where we are in the same situation of knowing nothing about the base class/interface definition. end rationale]

    "Generic type definitions" includes generic interface definitions, and "any implemented interfaces" also applies to your interface IOverload<TBase> : TBase scenario because I.8.9.8 says, "An interface type implements zero or more other interfaces." The rationale does not directly apply to your scenario, but if you were allowed to define both .class interface I`1<T> extends !0 and .class C`1<T> implements class I`1<!0>, then C<T> would be required to implement T and this would be impossible to verify at definition time.

    Thus, the C# compiler alone could not be changed to permit the kind of interface definition you suggest. I don't know if there is a workaround.


    • Edited by ranta Tuesday, October 6, 2015 6:41 PM <sup> is apparently not supported here
    Tuesday, October 6, 2015 6:41 PM
  • "then C<T> would be required to implement T"

    Actually that is exactly what I'm trying to accomplish.

    Note the "where TBase : interface". I do not want to bind to a specific interface I only want that part to validate whether TBase is an interface (To make sure TBase is an interface). Personally I find it problematic to extend many interfaces (readability), so I try to have less interface declarations.

    BTW: I know this isn't possible, just wanted to know if anyone could supply me with a good workaround for this problem. My classes now implement TBase and IOverload<TBase>  directly, which is cluttering my class inheritance structure. (TBase and IOverload<TBase> are not real classes, merely chosen to illustrate the problem)

    Tuesday, October 6, 2015 10:39 PM
  • BTW: I know this isn't possible, just wanted to know if anyone could supply me with a good workaround for this problem. My classes now implement TBase and IOverload<TBase>  directly, which is cluttering my class inheritance structure. (TBase and IOverload<TBase> are not real classes, merely chosen to illustrate the problem)

    Hi Crispy76,

    As you said, it isn't possible. So this is by design, as far as I know, there is no any workaround for this problem. And it is the bottom of developing program language. If you must have a workaround, I would suggest you go to UserVoice site to submit this issue. Thanks for your understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 14, 2015 8:11 AM