[Mono-list] Why are extension method dependent on .NET 3.5?

Dan Shryock dan.shryock at gmail.com
Thu Apr 16 19:42:07 EDT 2009


2009/4/16 Jiří Zárevúcký <zarevucky.jiri at gmail.com>:
> Hello, I don't know whether there is some technological concept behind
> this, but I always found that fact very annoying.
> As far as I know, extension methods are purely a syntactic sugar,
> aren't they? As such, there should be no runtime dependence on the
> .NET 3.5, because everything I write using an extension method can be
> very simply written as a normal static call. Neither should it have
> (e.g. there is no reason for) compile-time dependence on
> System.Core.dll. Can someone explain to me, then, why is it dependent?

The reason for requiring .NET 3.5 is because of the dependency on
System.Core.dll

The reason for the dependency on System.Core.dll is that the compile
needs some way to mark a method as an extension method when you add
the "this" keyword to the argument.  Having the keyword in the source
isn't enough because there are libraries which provide extension
methods, so there needs to be some sort of metadata attached to that
parameter for the compiler to know about the difference.  The metadata
that the compiler attaches is the attribute
[System.Runtime.CompilerServices.ExtensionAttribute] which happens to
reside in System.Core.dll thus causing the dependency on both that dll
and the 3.5 runtime.

Having said all of that, you can get arround the problem by providing
your own implementation of
System.Runtime.CompilerServices.ExtensionAttribute like this:

using System;

namespace System.Runtime.CompilerServices{
	public class ExtensionAttribute:Attribute{}
	}

By itself that isn't enough because it will conflict with
System.Core.dll, so you have to remove that dependency.  For gmcs, I
have done that by adding the -noconfig parameter to the compiler, and
then manually adding in all default assemblies that you use, including
-reference:System.dll which is normally automatically referenced.

Hope this explaination helps.

Dan


More information about the Mono-list mailing list