0

Is there any way to override autoproperty functionality in a derived class from the base class? For Example:

class Foo
{
    public override void OverrideSetSomeHow()
    {
    //do something
    }
    public override void OverrideGetSomeHow()
    {
       //do something
    }
}

class Bar : Foo
{
    public string Name { get; set; }
}

class SomeOtherClass: Foo
{
    public Guid Id { get; set; }
}

the desired effect would be that the get and set call the overridden methods in the base class (or however it would need to happen). i would like to enforce the functionality for get and set in all the derived classes without expecting other people using the code to know what the hell its doing, and it would look a hell of a lot cleaner than putting all of the identical logic in a bunch of different places.

derek
  • 123
  • 2
  • 4
  • 15
  • What you're trying to do it not possible. – MarcinJuraszek Dec 03 '15 at 01:27
  • i figured as much...but figured id give it a shot. thank you – derek Dec 03 '15 at 01:29
  • I'm at a loss as to what you're trying to accomplish. Could you please provide an example use-case of this, and maybe we could help you do it in a different way? –  Dec 03 '15 at 01:35
  • @Micky INotifyPropertyChanged requires properties with back variables and code within the set to raise the property changed event – Mick Dec 03 '15 at 01:45
  • im using MassTransit(rabbitmq) and mongo, between the two of them, serialization is a nightmare. my base class has a Dictionary Metadata, the getters and setters on the derived class just add to or get the object from the Dictionary using the property name as the key. id like it this way so that the objects in the dictionary are still enforced by POCO's and there is no room for user error. i can easily cast the base object to the derived one and back down and to another if id like and all the properties on the object do is check the dictionary. – derek Dec 03 '15 at 01:46
  • I have this working already by just defining the functionality in the get and set of each property...but its ugly, and leaves room for another developer on the team to screw it up – derek Dec 03 '15 at 01:50
  • You could create custom code analysis rule that enforces the property implementation you're after. Also this might be useful... http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist – Mick Dec 03 '15 at 01:52
  • Also potentially useful ... http://stackoverflow.com/questions/30103421/inotifypropertychanged-feature-on-auto-properties – Mick Dec 03 '15 at 01:54
  • ok, that was my initial thought, to just use a generic method on the base class that is called from the getter and setter. and code analysis to enforce its usage. thank you for the input. i figured that it was not possible, but im still kind of a newb, so i learn new shit all the time. haha – derek Dec 03 '15 at 02:00

1 Answers1

1

Not sure I get what you need, but if what you are trying to achieve is intercept calls so you can do additional work, I'd recommend you look at Castle DynamicProxy which allows exactly that.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • AMAZING! Thank you. I was able to use the IInterceptor to grab the value being set, get the invocation target(the derived class) and add the value to the dictionary(in the base class),set the value being set to null (now that its in the dictionary), all while keeping the default get/set on the auto properties. – derek Dec 03 '15 at 06:15