Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class declaration "from metadata"

I've noticed in Visual Studio some classes have an option that if you look at their definition, you don't actually see the definition only the class's declaration and some documentation, for example: enter image description here

Does anyone know how to make my classes lead to a similar file? Thanks

like image 811
Pankov Avatar asked Dec 03 '22 14:12

Pankov


1 Answers

I think some basic has to be explained here.

  • Source code is bunch of C# files (.cs). This is where code of your classes is

  • When you compile source code you will get an assembly (.dll). Assembly contains metadata about your classes and compiled binary code, but not actual source code.

  • When you compile your source code, Visual Studio produces also .PDB file along with your assembly. PDB files allows you to see the source code of the assembly. PDB files are necessary for debugging. It is somewhat similar to javascript source maps.


Now, when in Visual Studio you Go to Definition of a class or a method , then following can happen:

  1. The class is in your solution -> you are navigated to the source code file (.cs)

  2. The class is defined in a referenced assembly and PDB file is available -> you are navigated to source code extracted from the PDB file. You can debug it, (however, you cannot edit it).

  3. The class is defined in a references assembly and PDB file is NOT available -> you are navigated the assembly metadata. (this is what happend on the picture you've posted)

So answer to your question is: isolate your assembly from .PDB and source code. However, you should be aware, that there are tools, that can reverse engineer C# code from the binary code that is in the assembly. It will be not exactly the same as your original source code but very similar.

like image 138
Liero Avatar answered Dec 22 '22 07:12

Liero