Question

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.

Was it helpful?

Solution

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;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top