1

Let's say we have a hierarchy of interfaces: IBaseThing<T> and IChildThing:IBase<ChildThing>. Also there are two classes like this:

internal abstract class Base<T>
{
    public abstract IBaseThing<T> Thing {get;}
}

and

internal class Concrete:Base<ChildThing>
{
    public override IChildThing Thing
    {
        get
        {
            return GetChildThing();
        }
     }
}

What will happen is as bellow:

'Concrete' does not implement inherited abstract member 'Base.get'

I want to skip casting (IChildThing)this.Thing all over Concrete class. What I supposed to do?

3
  • IChildThing : IBase<ChildThing> is an incredibly smelly interface. May be you wanted ChildThing : IBase<IChildThing>.. Cant be sure.. Commented Dec 9, 2013 at 10:40
  • Wrong assumption nawfal. IThings are not T. Commented Dec 9, 2013 at 11:06
  • Reza, kindly post the complete example (yet only what is required) and the use case u r after. We could help you better. Otherwise its all assumptions. Commented Dec 9, 2013 at 11:23

2 Answers 2

2

You can't do that. The type system is rightly objecting to you attempting to change the method type in the overriding class. IChildThing might derive from IBaseThing, but it is not the same thing. For example, you might add some more methods to IChildThing later on. Then one class (Concrete) would be promising a returned object with a different signature than the base class - that's a type error.

If you need to refer to a IChildThing in some cases, how about something like:

internal class Concrete : Base<ChildThing>
{
    public override IBaseThing<ChildThing> Thing
    {
        get { return ChildThing; }
    }

    public IChildThing ChildThing { get; set; }
}

If all you have is a Base though, then you can't get at the IChildThing safely. That's the nature of a strongly typed system like this. If that's a problem, then if you gave a more concrete example I might be able to suggest how to restructure to avoid that problem.

Sign up to request clarification or add additional context in comments.

6 Comments

I cant figure out the Thing property usage now! I'm declaring Thing as a property to be accessible from other classes. Internally this works, but actually you ignored the property philosophy. right?
What do you mean I ignored the "property philosophy"? I'm not familiar with what you mean.
We specify a property to provide get and set for other classes, that means private property is not reasonable, instead we can use a private field. In your solution, IBaseThing<ChildThing> Thing is not consumable as IChildThing in other classes without casting which is not what I'm looking for.
I have updated my answer, so that other classes can get at the IChildThing, as well. However, you have to understand that this is done separately from the inherited method - classes that are just expecting a Base<T> won't be able to get at it, and that's just how the type system works.
I know! I'm looking for a solution to force developers override the get method with proper type. Following your instruction, a developer may forget to write second ChildThing property.
|
2

Try this solution:

public interface IBaseThing {}

public interface IChildThing : IBaseThing {}

internal abstract class Base<T> where T : IBaseThing
{
    public abstract T Thing {get;}
}

internal class Concrete:Base<IChildThing>
{
    public override IChildThing Thing
    {
        get
        {
            return GetChildThing();
        }
     }

     public IChildThing GetChildThing()
     {  
        // Your impelementation here...
        return null;
     }
}

7 Comments

Isn't this just the same as the question?
This is exactly what I want to do, but I've got the above error.
About the update: Things have to be interfaces. There are some Internal implementation for these public interfaces.
@Alexander my comment was before your update. Btw I recommend changing type definition to class Base<T> where T : IBaseThing or something similar. A constraint would be nice.
@Reza Owliaei, ok, seems, i don't completly clear your question. Please add definition of IBaseThing, IChildThing interfaces and ChildThing class.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.