I have this property in a class:
public string fooo { get; set; }
I need to rewrite it in something like that :
[BsonElement("fooo")]
public string fooo{ get; set; }
I have to do it for a LOT of properties, so I would like to use the 'Find a replace feature' in Visual Studio.
I use the following macro :
Search : public
Replace : [BsonElement("")]\n\t\tpublic
How is it possible using regex to get dynamically the fooo name and put it in the BsonElement property ? And globally, is it possible to have a link to documentation about this, haven't find it.
Here's a solution for properties formatted like public string fooo { get; set; } (whitespace is important: one-liners, with the space after property name)
Search:
(public) (\w+) (\w+) { get; set; }
Replace:
[BsonElement("$3")]\n\t\t$1 $2 $3 { get; set; }
Explanation:
$1, $2, $3 are groups captured in ()
\w is equivalent to [a-zA-Z0-9_], so (\w+) will match any type name and property name
Find: (\s*)public string (\w+) { get; set; }
Replace: $1[BsonElement("$2")]\n$1public string $2{ get; set; }
This solution preserves the whitespace (tabs, etc) before the definition
Image for your convenience
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With