Get properties from a .cs class file on disk

I am writing a VSIX extension for Visual Studio. With this plugin, the user can select a class file from his decision browser in VS (so that the actual file is .cssomewhere on the disk), and then perform a specific action on this file by calling my VSIX code through the context menu item.

My expand VSIX need to know which properties publicand internalbelong to the selected file type.

I am trying to solve this using a regex, but I'm kind of stuck in it. I can’t figure out how to get class property names. He is finding too much now.

This is the regex that I still have:

\s*(?:(?:public|internal)\s+)?(?:static\s+)?(?:readonly\s+)?(\w+)\s+(\w+)\s*[^(]

Demo: https://regex101.com/r/ngM5l7/1 From this demo I want to extract all property names, so:

Brand,
YearModel,
HasRented,
SomeDateTime,
Amount,
Name,
Address

PS. I know that regex is not suitable for this kind of work. But I think I have no other VSIX extension options.

+4
source share
1 answer

how to get only class property names.

This template is commented out, so use IgnorePatternWhiteSpaceas an option or delete all comments and join the same line.

But this template gets all your data, like you , shown in the example .

(?>public|internal)     # find public or internal
\s+                     # space(s)
(?!class)               # Stop Match if class
((static|readonly)\s)?  # option static or readonly and space.
(?<Type>[^\s]+)         # Get the type next and put it into the "Type Group"
\s+                     # hard space(s)
(?<Name>[^\s]+)         # Name found.
  • Finds six matches (see below).
  • (?<Named> ...), mymatch.Groups["Named"].Value .
  • "" "" .
  • .

( ) :

Match #0
             [0]:  public string Brand
  ["Type"] → [1]:  string
  ["Name"] → [2]:  Brand

Match #1
             [0]:  internal string YearModel
  ["Type"] → [1]:  string
  ["Name"] → [2]:  YearModel

Match #2
             [0]:  public List<User> HasRented
  ["Type"] → [1]:  List<User>
  ["Name"] → [2]:  HasRented

Match #3
             [0]:  public DateTime? SomeDateTime
  ["Type"] → [1]:  DateTime?
  ["Name"] → [2]:  SomeDateTime

Match #4
             [0]:  public int Amount;
  ["Type"] → [1]:  int
  ["Name"] → [2]:  Amount;

Match #5
             [0]:  public static string Name
  ["Type"] → [1]:  string
  ["Name"] → [2]:  Name

Match #6
             [0]:  public readonly string Address
  ["Type"] → [1]:  string
  ["Name"] → [2]:  Address
+3

Source: https://habr.com/ru/post/1676563/


All Articles