当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Partial Class with Virtual Properties?

问题描述:

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.

网友答案:

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;
   }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: