tags:

views:

378

answers:

6

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.GetMethods()

to get the Class and methods in an Assembly.

Similarly how to get all the classes present within a C# file(.cs file).? I need to get the classes in a .cs file from which i can easily get the methods within them and further details like parameters of methods etc.

+2  A: 

Short of using a C# parser, there's no direct way of doing it. You could compile the .cs file using CSharpCodeProvider (which only works if the file compiles on its own and you can tell all the referenced assemblies to the compiler) and use reflection on the resulting assembly.

Mehrdad Afshari
but what if my project is split into multiple .cs files and only compiling all of them gives me an Assembly. I actually need for each individual .cs file.
pragadheesh
I believe you should use a C# parser to get these data from semi-valid C# programs (like Visual Studio does that with incomplete C# sources).
Mehrdad Afshari
A: 

The compiler erases all notions of a codefile from your code as it is compiled. That being said perhaps it is possible to retrieve the information you want from debugging symbols if they are available in your assembly.

Andrew Hare
A: 

From with in the class you can always call

this.GetType()

or outside the class you can always call

obj.GetType()

however when you compile an application, which is required for reflection to work, you can no longer get their definitions by file.

Nick Berardi
A: 

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.

rein
A: 

I recommend you to use a parser generator tool to generate a quick c# parser, you can use Antlr.

Also you can check this and this

Ahmed Said
A: 

First of all, there is no such thing as the .cs file in which a class is defined. A class can be marked as partial and parts can be defined in several .cs files.

When you compile with debug information, the filenames for each method remain in the assembly (for each line of the source file, the corresponding IL commands are tagged).

Unfortunately, I don't know an easy way to get to that information from within the running application (without parsing the assembly file manually).

If you are safe calling the method, you can call it and in parallel construct a stack trace (from another thread) - in the StackFrame object you will find the original file name. But this is slow (as you have to call every method just to find that the filename is different) and risky (what if the method formats your hard drive?).

So, the only way you could go is try to parse the .cs file with a parser like AntLR yourself.

mihi