.NET classes and their source code - c#

When I'm writing a C# (or any .NET programme) I use methods and classes. Most of the code I use is calling methods from the .NET classes. Is it possible (purely out of curiosity) to see the actual source code for these classes?
I know MSDN has full listings of the classes, their properties and their methods. But I would like to see the code.

Yes, it is:
Browse the .NET Framework source code online, with search and navigation powered by Roslyn.
See details at the .NET Framework blog...

Yes it is possible. See here for more info:
http://weblogs.asp.net/scottgu/archive/2008/01/16/net-framework-library-source-code-now-available.aspx

You can also run a disassembler (such as the one in Reflector) over the base class libraries and view code that way, if you don't want to configure your dev environment. You won't get real variable names or comments, but for isolated viewing this can be easier.

Yes, Microsoft has released the source .NET.
This article should help get you started.

The link mentioned in the accepted answer (https://referencesource.microsoft.com/) only contains .NET source up to version 4.8. Development is now done on Github, where you can find (literally) up-to-the-minute versions of the source files.
The dotnet Github organization (the .NET Foundation) has many of the repos relating to .NET, including core, the CLR runtime, aspnetcore, and a bunch of others. Most of the source code is organized into Visual Studio projects and solutions, so you can import them easily.

Related

How can a Custom Framework be used instead of .NET in Visual Studio for C# projects?

Disclaimer: I have only two day's experience trying to learn modern MSBuild techniques, but have used C# and VS for many years largely avoiding project file details outside of what the UI provides.
I am creating experimental DLLs that intentionally name-match and somewhat interface-match several of those found in the .NET Standard. For instance a custom System.IO.dll may partially match the public interface of .NET's System.IO.dll, same with mscorlib.dll, etc.
The goal is to be able to create a new C# project (File->New->Project->...) and have it link to these alternatives DLLs and not the official DLLs. This has been done successfully from the command-line using csc.exe, but not yet from within VS.
Using detailed build output verbosity in VS (Tools->Options->Projects and Solutions->Build and Run) shows that the numerous DLLs within .NET are added to the csc.exe command-line. Perhaps if this could be prevented all would be well. One failed attempt to do so involved removing the <TargetFramework> altogether.
Maybe registering a "custom framework" with Visual Studio would allow a custom value to be used within csproj, e.g.<TargetFramework>mydotnet</TargetFramework>?
All just guesses.
It seems MSBuild is very rich and would allow for multiple ways to achieve such a thing. I am completely receptive to learning one or more of these techniques, since one may provide advantages for future build customization plans.
How can a Custom Framework be used instead of .NET in Visual Studio for C# projects?
To use a Custom Framework instead of .NET in Visual Studio for C# projects, you have to do a lot of things, Registering Framework, Create a Template for Project and so on. So, Are you really writing your own implementation of the framework? If you just want to add your own libraries to an existing Framework, you can refer to following thread:
Registering Extensions of the .NET Framework
Besides, there is a document about How to: Add a Custom ASP.NET MVC Test Framework in Visual Studio, you can check it for details.

Saxonica 9.7 HE in C#

I'm trying to get Saxon 9.7 HE up and running in C#, and failing miserably.
As per instructions, I am downloading and running https://sourceforge.net/projects/saxon/files/Saxon-HE/9.7/SaxonHE9-7-0-14N-setup.exe/download
This installs a bunch of files into C:\Program Files\Saxonica\SaxonHE9.7N.
In the bin folder inside this one, the following files are present:
No matter which DLLs I add as references to my project, the "using Saxon.Api;" reference fails to resolve.
Am I using the correct DLLs here? Is it some sort of .Net version mismatch? My class library project is set to .Net 3.5 currently.
Thanks for any help
The minimum supported version of .NET for Saxon is indeed 4.0.
The bug is in the documentation which will be updated shortly: See bug issue: https://saxonica.plan.io/issues/3085
Thanks for reporting the issue.
Some guess work here. But the dependency on .net 4 is most likley from the compiling of the C# wrapper over the saxon java library that was converted using IKVM. IKVM says it only requires .net 2.0, so I think the only place that dependancy can come from is the saxon9he-api.
You should be able to build this yourself, its quite thin.
https://dev.saxonica.com/repos/archive/opensource/latest9.7/hen/csource/api/Saxon.Api/
Otherwise if its a show stopper ask Michael Kay (the author), he's pretty active on stack overflow.
Updated
I was curious so I checked, the .Net 4 dependency seems to come from the saxon9he-api (which i'm fairly certain could be re-compiled under 3.5 with no/minimal changes), the same for the Query and Transform exe's.

upgrade program for converting vb6 app to C#

What is the best pathway to achieve this?
I know that VS2005 contains an upgrade mechanism. Do any later versions of VS contain this?
Microsoft has devoted a site to VB6->.NET migration.
They recommend a Free tool from ArtInSoft.
However I'm not sure I'd like to maintain a .NET application written in VB6-style. But on the other hand a tool could give you a good start and you can refactor the result where the tool does not produce code of your liking.
In the latest release of Visual Studio, the VB6 Migration wizard is now missing from the IDE.
A good external tool to perform a migration is VBUC .
One way to do this, which was used successfully in one of my previous teams, is:-
use the free Microsoft tools for upgrading from VB6 to VB.NET (e.g. http://msdn.microsoft.com/sv-se/vbrun/ms788233)
compile the resulting code into assemblies
decompile the assemblies into C# using a tool such as .NET
Reflector, ILSpy, etc.
cover the code in unit tests
refactor until the code is managable (the initial code is likely to
be quite ugly)

How to find out how libraries work together?

I'm tasked to replicate functionality from an existing application. This application relies on .NET managed assemblies accessible from C#. I can import those DLLs in my new C# project but there is no documentation on how to use them.
Yes, this is labeled as an "SDK", but does not contain examples or documentation. Any pointers on how I should proceed?
I thought about creating stub assemblies and monitor their usage from the original application but this involves a lot of code. Is there maybe a tool could do it for me?
Using the Reflector is the best way to do that. You can see what calls what and examine the dlls in a good and easy way.
You can also try Cecil
They both are great tools for inspecting managed dlls

How to use Microsoft.Scripting.Hosting?

To embed some IronPython Code into C# I want to use the ScriptEngine
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
I found the reference for IronPython, but where is the necessary reference for Scripting.Hosting? I can't find it within VisualStudio 2008, targeting .Net 3.5.
The DLR is actually broken into 2 parts:
the "inner layer" which is included in .NET 4.0 and is part of System.Core.dll (and on downlevel platforms is available as Microsoft.Scripting.Core.dll)
the "outer layer" which is available on CodePlex and ships w/ IronPython and IronRuby and is in Microsoft.Scripting.dll and Microsoft.Dynamic.dll. This part does not ship w/ .NET 4.0.
It looks like this is part of the DLR binaries ... more information here:
https://blogs.msdn.com/seshadripv/archive/2008/06/24/how-to-write-a-simple-dlr-host-in-c-using-hosting-api.aspx
Microsoft.Scripting is part of the Dynamic Language Runtime. The Dynamic Language Runtime is part of .NET 4, IronPython and IronRuby and is also available standalone.
John Lam, the lead developer of IronRuby, created a little library called repl-lib, which implements a simple editor/REPL hybrid for IronRuby and IronPython which you can just drop into your application. In a blog post, John Lam demonstrates how he used repl-lib to add scripting to the Witty .NET Twitter client in 8(!) lines of code (plus 90 or so lines of XAML for the REPL UI and the REPL button).
That blog post is based on a presentation at TechEd 2009, which I unfortunately don't have access to, but if you do, you should check it out.
Be sure to also check out the forks of repl-lib, especially Jim Deville's.
I suggest that you use NuGet instead of managing the binaries yourself.
This worked for me.
http://nuget.org/packages/DynamicLanguageRuntime.Net40.Unofficial

Categories