Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to have a getter/setter template in c#?

Tags:

c#

getter

setter

I want to apply this:

private string _name;

public string name
{
    get { return _name; }
    set { _name = Fix(value); }
}

to all string the members of a class, and don't want to repeat the same code for all the class members.

An obvious solution would be to put that code on a class to handle the problem and declare all string members as: myString instead of string, however that would mean that I would have to access the main class members like this: email.fixed instead of just email.

So I was wondering, is there is some kind of template I can define and then apply easily?

like image 864
george b Avatar asked Feb 20 '14 18:02

george b


2 Answers

You could create a Code Snippet for Visual Studio to handle building a property this way.

MSDN includes documentation on Creating a Code Snippet, which can include replacement parameters (the name).

like image 70
Reed Copsey Avatar answered Oct 21 '22 22:10

Reed Copsey


You might want to research Aspect Oriented Programming, which allows you to easily do things like this.

http://www.codeproject.com/Articles/337564/Aspect-Oriented-Programming-Using-Csharp-and-PostS

like image 23
Matt Cruikshank Avatar answered Oct 21 '22 20:10

Matt Cruikshank