Using extension methods in .NET 2.0?
Asked Answered
P

1

32

I want to do this, but getting this error:

Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff]

I have seen some answers here that says, you have to define this attribute yourself.

How do I do that?

EDIT: This is what I have:

[AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )]
public sealed class ExtensionAttribute : Attribute
{
    public static int MeasureDisplayStringWidth ( this Graphics graphics, string text )
    {

    }
}
Pathogen answered 5/10, 2009 at 21:54 Comment(1)
No; you need two classes; one for the attribute; one for the extension method(s); will update.Helping
H
61

Like so:

// you need this once (only), and it must be in this namespace
namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
         | AttributeTargets.Method)]
    public sealed class ExtensionAttribute : Attribute {}
}
// you can have as many of these as you like, in any namespaces
public static class MyExtensionMethods {
    public static int MeasureDisplayStringWidth (
            this Graphics graphics, string text )
    {
           /* ... */
    }
}

Alternatively; just add a reference to LINQBridge.

Helping answered 5/10, 2009 at 21:55 Comment(7)
Thanks Marc, it was actually your post that I read. I just tried but got this: Error 1 Extension methods must be defined in a non-generic static class, where I have a method like this: public static int MeasureDisplayStringWidth ( this Graphics graphics, ... )Pathogen
Also ExtensionAttribute can be any name, right? And why inherit from Attribute?Pathogen
You need to inherit from Attribute for it to be an attribute... and it needs to be called ExtensionAttribute so the compiler can find it. (That's what it expects it to be called.) Your error is probably that it's not in a static class.Esophagus
Thanks Jon, I see what you mean. Now this is what I don't understand. Where does my extension class method goes? Does the class go inside this ExtensionAttribute with my method?Pathogen
For libraries, is there any harm in declaring ExtensionAttribute as internal instead of public? (i.e. internal sealed class ExtensionAttribute : Attribute { }). Would this be better practice, to avoid conflicts if an application is using two libraries that both implement this trick?Marketable
@Marketable Unfortunately, that doesn't stop the defined in multiple assemblies warning (when using the library in a >= 3.5 program). I'm just compiling a separate library targeting 3.5 for now.Keynote
this error can also arise if you are using .Net version 2.0 when I switched to version 3.5 it was fixed regardless the fact that I had not added the Attribute and sealed class typeBounded

© 2022 - 2024 — McMap. All rights reserved.