views:

72

answers:

1

I have a class formed by two partial classes.

One created by ORM code generation and one for extensions.

In this particular instance, I need to override one of the properties generated by the partial class because I need to do some validation on it first.

Is it possible to use my extension class to kind of override the property of the code generation partial class?

+2  A: 

No, not possible. If you are the owner of the code-generation, you should put in hooks for handling that scenario. For example, sqlmetal.exe for LinqToSql produces partial classes wherein each property setter looks a bit like this:

if (this.myProperty != value) 
{
    this.OnMyPropertyChanging(value);
    this.SendPropertyChanging();
    this.myProperty = value;
    this.SendPropertyChanged("MyProperty");
    this.OnMyPropertyChanged();
}

Of course, the generator also creates those property-specific changing/change methods, but they declare those as partials:

partial void OnMyPropertyChanging(string newValue);
partial void OnMyPropertyChanged();

With this setup, it's obviously quite easy to tap into these events for the extension partial class.

Kirk Woll