How to get Classes and methods from a .cs file using Reflections in C#.?

前端 未结 6 1960
执笔经年
执笔经年 2020-12-12 04:06

How to get the classes that are available in a \'.cs\' file.? Like we can get the classes and methods in an Assembly using,

Assembly.GetTypes() and Type.GetM         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 04:42

    I've done this previously by invoking the C# compiler, compiling the C# file and then using reflection on the outputted type. This is possible if the C# file is a standalone file and doesn't have any dependencies.

    However, the correct way would be to use a parser - something which isn't that easy to do. There are a couple of options available, MinosseCC being one of them.

    Incidentally, C# 5.0 will make it a lot easier to compile code on the fly by being able to compile a String and getting back executable code. Can't wait for this - it's sure to confuse everyone that reads my code.

提交回复
热议问题