简体   繁体   中英

Override a method from a base class that explicitly implements an interface

[Edit]

Due to a little more complexity than was needed on the original post -- my apologies for that-- I'll try and simplify things a bit.

Originally, I had a base class that looked like this:

public class OurViewModel
{
  public bool Validate(ModelStateDictionary modelState){...}
}

My class inherited and hid that method, using the new keyword:

public class MyViewModel : OurViewModel
{
  public new bool Validate(ModelStateDictionary modelState) {...}
}

Meanwhile, the IValidatableObject interface has appeared:

public interface IValidatableObject
{
  IEnumerable<ValidationResult> Validate(ValidationContext validationContext);
}

Now, OurViewModel has (sadly) also been changed. It looks like this:

public class OurViewModel : IValidatableObject
{
  IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext) {..}
}

What I need to do now is again hide the Validate method in MyViewModel, but I can't figure out how do that with the current arrangement. Is there a good way of going about this?

(FWIW, I agree that adding a new interface to a base class is not the optimal way to have done things, but it's what's happened here.)


[Original Post]

I'm having trouble overriding a method in one of my classes. Someone changed the inheritance model of the base class from this:

public class OurViewModel : IntlViewModelBase<OurResources>

to this:

public class OurViewModel : IntlViewModelBase<OurResources>, IValidatableObject

IValidatableObject has a single method signature, Validate() :

IEnumerable<ValidationResult> Validate(ValidationContext validationContext);

OurViewModel had a Validate method that looked like:

public bool Validate(ModelStateDictionary modelState)

...and now is this after the change:

IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)

My class, meanwhile, inherits from OurViewModel . It used the new keyword to implement its own boolean version of Validate.

It seems I'm no longer able to hide the Validate method because my class does not implement IValidatableObject. What's the proper way now for me to override that method?

I may be missing the point completely, but this compiles fine for me (used stub classes for OurResources and IntlViewModelBase<T> ).

public class OurViewModel : IntlViewModelBase<OurResources>, IValidatableObject
{
    public bool Validate(ModelStateDictionary modelState)
    {
        throw new NotImplementedException();
    }

    IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
    {
        throw new NotImplementedException();
    }
}

public class MyViewModel : OurViewModel
{
    public new bool Validate(ModelStateDictionary modelState)
    {
        throw new NotImplementedException();
    }
}

如果要覆盖新的Validate方法(IValidatableObject.Validate),只需添加virtual关键字即可。

public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

I think I've munged this question enough, and have decided to try again with a simpler version provided here-- this one

Thanks to everyone who replied. Could an admin close this thread or aim/link it toward the new question? I'm not sure what the SO netiquette is on situations like these.

Few things :

1.

If for every method in your base class you want to have new implementation in derived class then there is no point in deriving from base. The main purpose of inheritance is code reuse. So you can simply remove the relationship from parent child.

2.

Lets assume that in your base class there are some methods which are actually inheriting. In that case I dont thing there should be a problem with marking Base class's Interface member with Virtual keyword and in derived class saying it as new.

--

If you are asking about different way to implement this situation then we need to know what's your aim. If the aim is to have derived member to have new definition then better way would be Explicit implementation of the interface in derived class.

Please mention the exact problem you are facing ? is it that you are not able to use new keyword or you are asking different ways to solve this situations.

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