0

I noticed that some of the Microsoft .NET Framework reference source doesn't seem to be valid or at least complete C# code.

Here's an example: https://referencesource.microsoft.com/#System.Xaml/System/Windows/Markup/StaticExtension.cs

Relevant code:

[MarkupExtensionReturnType(typeof(object))]
[TypeConverter(typeof(StaticExtensionConverter))]
[TypeForwardedFrom("PresentationFramework, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
public class StaticExtension : MarkupExtension
{
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    public StaticExtension();
    public StaticExtension(string member);

    [ConstructorArgument("member")]
    public string Member { get; set; }

    [DefaultValue(null)]
    public Type MemberType { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider);
}

Neither of the constructors nor the ProvideValue() method have bodies. As far as I know this isn't valid C# syntax as it is not an abstract class.

Since this is "reference source" - aren't you supposed to be able to read how it works in order to understand what it actually does? Without the bodies... that doesn't seem possible.

So I feel like I must be missing some other resource with more / complete code, misunderstanding something about C#, or misunderstanding what the reference source is for.


I have read that there are some oddities in the reference source regarding censorship of possible obscenities, but that doesn't seem to explain this. https://stackoverflow.com/a/33873904/3195477

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
  • This is not actual C# source used to build the assembly; it's decompiled. The comment at the top which includes the absolute path is a dead giveaway. Reference assemblies contain only metadata and no executable code. The reference source does not actually include "the" source for all assemblies; XAML/WPF is a big chunk that's missing. I don't know if it's ever been part of the reference source, [but it is not now](https://github.com/Microsoft/referencesource). – Jeroen Mostert Feb 13 '20 at 14:01
  • @JeroenMostert I understood that it was not "the" source of what is actually compiled into .NET, but I did think it was at least "representative" source of how the framework functions. – StayOnTarget Feb 13 '20 at 14:04
  • It used to be, but it has since been more or less frozen in place and retired to automatic generators while development has shifted to .NET Core. Unfortunately there's no archive (that I know of) that we could use to check if this particular bit of source has ever been available "properly" (once upon a time, this was just a snapshot of the .NET 2 Framework ("Rotor"), so it had no WPF at all). You could open an issue with the repo I linked to see if this can be fixed or bridged somehow with at least decompilations of the actual assemblies. – Jeroen Mostert Feb 13 '20 at 14:11
  • I just use ILSpy when I want to poke around the .Net source. –  Feb 13 '20 at 14:23
  • @Amy: you miss the comments that way, though, and there are some insightful ones in the reference source (for the source that is included). That plus the identifiers will have more meaningful names, which can help. Notwithstanding that ILSpy is a great way to get source fast. – Jeroen Mostert Feb 13 '20 at 14:36
  • None of the source for System.Xaml.dll is provided. Other examples are Microsoft.VisualBasic.dll, System.ServiceProcess.dll, etc. These decisions were made well before they decided to open-source the framework. Exactly why is but a guess. – Hans Passant Feb 13 '20 at 16:04

0 Answers0