Get a method's contents from a cs file

前端 未结 6 1759
孤独总比滥情好
孤独总比滥情好 2021-01-19 10:40

I have a requirement to get the contents of every method in a cs file into a string. What I am looking for is when you have an input of a cs file, a dictionary is returned w

6条回答
  •  無奈伤痛
    2021-01-19 11:15

    A custom-made parser will be the best option. As Blair stated in the comment to StingyJack, it is VERY difficult to parse code with regex. I've attempted it once, and, although it is possible to match balancing braces with NET, escaping comments and strings is much more complicated.

    A parser should make things much simpler. See antlr for a good generator.

    As for reflection, I believe you could attempt to compile the code (as long as you have all necessary dependencies) and then access it contents Reflector-like. But I'd go with the parser.

提交回复
热议问题