C # property override Set method

I have a class like the one below, I want to override the set value of the property "School, country .....", when someone sets the value, I don’t want to change the student’s class, but I need to do this in the base class and use him as a general method

public class Student : BaseClass { public String School { get; set; } public String Country{ get; set; } } 

namely: When someone sets Student.School="Harvard" I need to save it as Student.School="Harvard my custom value" ;

Note: Mostly calling OnPropertyChanged in the base class , not in the main class.

+4
source share
5 answers

If you want to do this with aspects, try Postsharp

+1
source

In principle, you cannot override a non-virtual property. You can hide it with another property with the same name in the derived class, but this will not give you the desired effect if any other code refers to your object by reference to the base class.

0
source
 public class Student : BaseClass { private string _school public string School { get { return _school; } set { if(value == "Harvard") value = "Harvard custom"; _school = value; } } public String Country{ get; set; } } 

Is that what you mean?

If the School property is in BaseClass , then you can either use the new keyword, or if you control BaseClass , then you can add the virtual to the School property there and override in the Student class.

0
source

It is simply impossible to accomplish just by changing the BaseClass . Think of it this way: if you could easily “annotate” automatic properties, then we would not need all these “useless tons” of manual property implementations for data model classes that implement INotifyPropertyChanged (the same for DependencyProperties).

You need to provide hooks in your subclasses that your base class can use. The PropertyChanged implementation that you already mentioned is one of the possible solutions, the other with a simple method call:

 public class Student : BaseClass { private string _school; public String School { get { return _school; } set { _school = value; DoMoreChanges(ref _school); // DoMoreChanges is defined in BaseClass } } public String Country{ get; set; } } 

If you have many subclasses that need it, you can use Visual Studio code snippets to create code or T4 templates .

0
source

Since your base class does not have these properties, you cannot modify them from the base class using standard OOD templates or principles.

Now, if you move the properties to the base class either as regular properties or virtual properties, you can change what you do in the given property block to do the extra work.

However, if you cannot transfer them to the base class, and you cannot change the Student class, as you seem to imply in your question, then you can encapsulate the student class in a new class, for example StudentProxy or something else, and then expose it to similar properties, which then invoke the student’s real class as you wish.

For instance:

 public class StudentProxy { private Student _student; public StudentProxy(Student student) { this._student = student; } public String School { get { return _student.School; } set { _student.School = value + " my custom value"; } } public String Country { get { return _student.Country; } set { _student.Country = value + " my custom value"; } } } 
0
source

All Articles