1

I was wondering if there is a way to hide inherited properties on sub types in C#, something like this for instance:

public interface ISupplierLogin : ISupplier
{
    [Obsolete]
    new string BusinessName { get; set; }

    [Obsolete]
    new long ABN { get; set; }

    [Obsolete]
    new DateTime DateRegistered { get; set; }
}

public interface ISupplier
{
    bool TradeOnly { get; set; }

    [Required]
    [DisplayName("Business Name")]
    [StringLength(25, ErrorMessage = "The maximum length of business name is 25 characters.")]
    [DataType(DataType.Text)]
    string BusinessName { get; set; }

    [Required]
    [DisplayName("Australian Business Number")]
    [ABNValidator(ErrorMessage="The ABN you entered does not appear to be valid.")]
    long ABN { get; set; }

    [Required]
    [RegularExpression("^[a-zA-Z0-9]+$", ErrorMessage="The username you entered does not appear to be valid. (a-z & 0-9)")]
    [StringLength(25, ErrorMessage = "The maximum length of username is 25 characters")]
    [DataType(DataType.Text)]
    string Username { get; set; }

    [Required]
    [StringLength(12, ErrorMessage = "The maximum length of password must be between 8 and 12 characters long.")]
    [DataType(DataType.Password)]
    string Password { get; set; }

    [DisplayName("Date Registered")]
    DateTime DateRegistered { get; set; }
}

Where I want to inherit my validation attributes in my view model, but only for two properties.

Is there anyway to achieve what I am trying to do neatly?

Thanks.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Alex Hope O'Connor
  • 9,354
  • 22
  • 69
  • 112

2 Answers2

4

You are misinterpreting the idea of inheritance: Basically when you have two classes A and B, and B is an exension of A, B will inherit all non-private elements of A; this is no different if you were using an interace and a class, two interfaces, etc.

Therefore in my opinion you should only use inheritance if you implement all fields, else you kind of defeat the purpose of inheritance and you're not following the object oriented principles.

What you can do however (not the best option, but it is possible) is use:

thow new NotImplementedException();

This make show to the end-programmer that he/she cannot use this property, as his or her application will throw an exception in the debugging stage.

Similarly you can use [Obsolete(..., true)], which will make the compiler throw an error on compiling; note the second parameter is set to true. Also, you can use [Browsable(false)] to hide it in IntelliSense, but this will still allow use of this property.


You can add this to a property, which will effectively disable most usages of your property. It will not actually hide it in the sense you want, but it does show a developer to ignore it.

[Bindable(false)]
[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

As far as actually hiding it goes, I don't think there is a possibility for that.

KilZone
  • 1,580
  • 9
  • 20
  • Yes that is the concept of inheritance, however I am trying to hide the properties from reflection, as to avoid errors when model binding. So I am really asking how to achieve something 'like' inheritance in this situation, but where I am hiding properties instead of adding them. – Alex Hope O'Connor Jul 16 '11 at 14:12
  • Well that's the whole problem with inheritance, there is no 'hiding' properties, only extending, but let me edit the answer with something I've found on the internet a while ago. – KilZone Jul 16 '11 at 14:18
1

I believe this previous question will give you the answers you seek.

How to hide an inherited property in a class without modifying the inherited class (base class)?

Community
  • 1
  • 1
Rig
  • 1,276
  • 3
  • 22
  • 43