I would like to be able to view the code running behind the scenes when I call specific functions in C# - is it possible to find and decompile these code libraries? MSDN often has usage examples and plain-text explanations, but in some cases I want to see code execution and which functions call each other.
As a specific example, I would like to crack open the System.Web.UI.Page class. How can I view the source code for this class, not the documentation?
You can download the .NET Framework Reference sources from here:
http://referencesource.microsoft.com/netframework.aspx
This includes most (if not all?) of the BCL libraries.. such as Dictionary, List, String, etc.
During debug mode on,if you want to see decompiled code on the current cursor position in visual studio 2022, press ctrl+F11
This will take you to the current disassembly.
Related
.NET Framework proposes "///" for documentation in code. However, when I see the .NET library, I see they are using normal comment starting with "//". Also, I see that due to that the APIs look clean. See below screenshot:
Notice that collapsed box shows only ellipses("...") and are placed in front of method declaration. But when I try to follow the same, I do not get the desired result. See below screenshot:
Notice that I get "//" characters along with ellipses("..."). Plus, I cannot get the field/method and comment to be placed on the same line.
How do I achieve the same result? Is there any trick that I am missing here?
However, when I see the .NET library
You're not looking at the actual source code. That's just a representation of the metadata, generated for you by Visual Studio. It looks like some documentation is being pulled in from somewhere (that doesn't happen on my box in the way I've just tried it, but that's a different matter). You can tell this isn't real source code because there are constructors declared with no body.
If you look at genuine source code, e.g. ReferenceSource for TcpClient you'll see triple-slash comments. Interestingly, the .NET Core code doesn't have any XML comments either, but I suspect that's because they're defined elsewhere, as part of .NET Standard. (There's an XML file in the dotnet-api-docs repo, but it's not immediately clear what that's generated from...)
When it comes to your own code, I'd strongly recommend just using the /// syntax. /** ... */ should work as well, but I don't remember ever seeing that in C# code. // is not a valid way of writing an XML comment in C#.
Is there a way to get MarkdownSharp (I'm using the NuGet package) to handle 'GitHub flavored Markdown (GFM)' and especially syntax highlighting of c# code, which (in GFM) is written like this:
```c#
//my code.....
```
So, if I pass Markdown formatted content to MarkDownSharp, containg a C# code block (as above) I want it to generate syntax highlighted html for that c# code. Any ideas? I know I can use the supported 4 spaces to indicate a code block, but again, I'm seeking a solution for getting it to support GitHub flavored Markdown.
I have made some light modifications to MarkdownSharp that will transform github flavored fenced code blocks
https://github.com/KyleGobel/MarkdownSharp-GithubCodeBlocks
```cs
Console.WriteLine("Fenced code blocks ftw!");
```
Would become
<pre><code class='language-cs'>
Console.WriteLine("Fenced code blocks ftw!");
</code></pre>
It handles the cases I needed to use, there are probably lots of edge cases though, feel free to fork/change/modify/pull request. Markdown sharp has plenty of comments and is all only one file, so it's not too bad to modify.
Here's the result: https://github.com/danielwertheim/kiwi/wiki/Use-with-Asp.Net-MVC
//D
As one can read in this post, GitHub relies on RedCarpet to render Markdown syntax.
However, Vicent Marti (Sundown (ex-Upskirt) and RedCarpet maintainer) states that the syntax highlighting is specifically handled by Pygments, a python library.
Back to your concern, I can think of several options to benefit from syntax highlighting from C#:
Try and build a compiled managed version of Pygments source code thanks to IronPython ("IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables.")
Port Pygment to C#
Use a different syntax highlighting product (for instance, ColorCode which is used by Codeplex...)
Then either:
Fork MarkDownSharp to make it accept plug-ins
Similarly to what GitHub does, use the managed syntax highlighting product and post process the Html generated by MarkDownSharp
By the way, as a MarkDown alternative, you might want to consider Moonshine, a managed wrapper on top of Sundown which is said to be "at least 20x faster than MarkdownSharp when run against MarkdownSharp's own benchmark app."
Does anyone know if there is a way to evaluate c# code at runtime.
eg. I would like to allow a user to enter DateTime.Now.AddDays(1), or something similar, as a string and then evaluate the string to get the result.
I woder if it is possible to access the emmediate windows functionality, since it seems that is evaluates every line entered dynamically.
I have found that VB has an undocumented EbExecuteLine() API function from the VBA*.dll and wonder if there is something equivalent for c#.
I have also found a custom tool https://github.com/DavidWynne/CSharpEval (it used to be at kamimucode.com but the author has moved it to GitHub) that seems to do it, but I would prefer something that comes as part of .NET
Thanks
Mono has the interactive command line (csharp.exe)
You can look at it's source code to see exactly how it does it's magic:
https://github.com/mono/mono/raw/master/mcs/tools/csharp/repl.cs
As you've probably already seen, there is no built-in method for evaluating C# code at runtime. This is the primary reason that the custom tool you mentioned exists.
I also have a C# eval program that allows for evaluating C# code. It provides for evaluating C# code at runtime and supports many C# statements. In fact, this code is usable within any .NET project, however, it is limited to using C# syntax. Have a look at my website, http://csharp-eval.com, for additional details.
Microsoft's C# compiler don't have Compiler-as-a-Service yet (Should come with C# 5.0).
You can either use Mono's REPL, or write your own service using CodeDOM
Its not fast but you can compile the code on the fly, see my previous question,
Once you have the assembly and you know the type name you can construct an instance of your compiled class using reflection and execute your method..
The O2 Platform's C# REPL Script Environment use the Fluent# APIs which have a real powerful reflection API that allows you do execute code snippets.
For example:
"return DateTime.Now.ToLongTimeString();".executeCodeSnippet();
will return
5:01:22 AM
note that the "...".executeCodeSnippet(); can actually execute any valid C# code snippet (so it is quite powerful).
If you want to control what your users can execute, I could use AST trees to limite the C# features that they have access to.
Also take a look at the Microsoft's Roslyn, which is VERY powerful as you can see on Multiple Roslyn based tools (all running Stand-Alone outside VisualStudio)
I want to learn if there is any library in .net to write c# scripts. Let me describe you with more detail, for example I have an application. When I run it, a c# editor will be opened end user will write some c# codes and when click run, this code should be evaluated and dom should be created after interpret my run time c# code will run. this is brief description of my mind...
I put together a little app called SimpleDevelop which uses CSharpCodeProvider to do what you describe. My understanding is that this (CodeDom) is deprecated and generally discouraged; however, it seems to work just fine for simple scenarios.
Basically, you want to use something like the CSharpCodeProvider. The Razor view engine in MVC essentially uses this to compile your code into an executable to run. If you want your user to be able to write code and then have it interpreted, you would start here. Please note though, this is an incredibly complicated and time intensive feat to get right; plus, linking in and executing foreign code dynamically is a security nightmare. Just be safe.
Are you looking for a test bench sort of?
I use LinqPad for that.
It is mostly a test bench for Linq queries, but I find it very useful for C# statements and mini programs and such.
Check out the System.CodeDom namespace.
This article contains lots of useful information: http://www.developerfusion.com/article/4529/using-net-to-make-your-application-scriptable/2/
You can use the Compiler namespace and compilate the code at runtime. Take a look here for an explanation on how to do it.
I have created an application which will run c# like script without using visual studio.
It is on https://sourceforge.net/projects/csharpquickcode/
I'm wondering if anyone has done this already.
I want to format C# source code in HTML. But with a twist! I want to turn the names of all types and methods that appear in the code into hyperlinks to the MSDN Library documentation of the types and methods.
To do a good job, the data types of variables and expressions needs to be known, just like how the C# compiler does it. So it's a tall order. If something like this is not available, please point me to any free libraries that can generate a parsed tree of the C# source code in sufficient detail to do this task. (In fact, I'd like to know about such a standalone parser library even if the full solution I am asking for already exists.)
This kind of utility might benefit blogs and forums -- maybe even Stack Overflow!
Have you checked out Docu? It's an open source library that converts .net documentation into HTML documents.
I'd suggest using the Visual Studio SDK.