0

Is it possible to have virtual properties within a partial class? In my scenario, we have auto-generated classes that are used by our Micro-ORM and map exactly to our database tables.

We often wish to extend these classes, however, so using partial keyword is absolutely fine in this case.

I have a situation, though, whereby I want to override the auto generated getter in a partial class.

eg:

public partial class MyClass {
   public int MyProperty{ get; set; }
}

.. I'd like to override the get and implement some custom logic without manipulating the auto-generated code. This is vital.

Thanks.

pierre
  • 1,235
  • 1
  • 13
  • 30
  • Can you not use a partial method? Have it set the property. – Oded Feb 12 '13 at 13:46
  • So you have autogenned non-virtual properties with no obvious extensibility hook and you want to "override" those with your own custom implementation in a select few cases? – Jon Feb 12 '13 at 13:48
  • have you considered wrapping that propery in a new propety, and add the logic you need? – Jens Kloster Feb 12 '13 at 13:53
  • I don't see any way that is possible using partial classes. You could however make the new class inherit from the auto-generated one and simply "overwrite" the property with the `new` keyword. Then it would depend upon which class instance you queried for the value returned. – John Willemse Feb 12 '13 at 13:53
  • I should say that I would be happy to change the template for auto-generated classes to use virtual or some such if needed. I appreciate that just now, it would appear they classes are not suited to this type of thing. – pierre Feb 12 '13 at 14:33

1 Answers1

0

Maybe you can modify autogenerated classes to put gets and sets in other file. Example:

// file: MyClass_1.cs
public partial class MyClass
{
   public int MyProperty
   {
      get { this.MyPropertyGet(); }
      set { this.MyPropertySet(value); }
   }
}

And in other file:

// file: MyClass_2.cs
public partial class MyClass
{
   private int _myProperty;

   private int MyPropertyGet()
   {
      return _myProperty;
   }

   private void MyPropertySet(int value)
   {
      _myProperty = value;
   }
}
oarrivi
  • 191
  • 9
  • This would be a viable solution, though I will have to alter the auto-generated code creation to accommodate such a change. – pierre Feb 12 '13 at 15:59