-1

I am trying to implement an extension for any c# object using extensions.

My method will use an object X that I cannot modify and takes the type of the object as an argument. Problem is I want to keep this object; I don't want it to be garbage-collected.

This is my current code, but I cannot declare an extension method in a non-generic static class (Extensions_S<T> is NOT allowed. Compiler's error message is "extension method must be defined in a non-generic static class").

namespace Lib
{
    public static class Extensions_S<T>
    {
        private static X generateA_X = null;
        public static A generateA<T>(this T value)
        {
            if (generateA_X == null)
                generateA_X = new X(typeof(T));

            return generateA_X.from(value);
        }
    }
}

generateA_X is a actually the equivalent of a static method variable. I want a different instance for each type T which calls this extension method. This is why I am trying to make Extensions_S generic.

My question is: Is there a workaround?

KwentRell
  • 416
  • 1
  • 4
  • 17
  • 1
    Check [http://stackoverflow.com/q/6096299](http://stackoverflow.com/q/6096299) and [http://stackoverflow.com/q/2618271](http://stackoverflow.com/q/2618271) – krlzlx Feb 09 '17 at 11:18
  • What's the real problem you are trying to solve? – Ondrej Tucny Feb 09 '17 at 11:19
  • 5
    The parameter `T` on `Extension_S` isn't used anywhere so it looks like you can just remove it. – Lee Feb 09 '17 at 11:20
  • 2
    Your title does not match the genericity of your class. – H H Feb 09 '17 at 11:20
  • make static class non-generic: `Extensions_S` -> `Extensions_S`. It is ok to have generic methods (`generateA`). – ASh Feb 09 '17 at 11:21
  • Your class doesn´t need the generic type parameter, however your method does. – MakePeaceGreatAgain Feb 09 '17 at 11:21
  • 2
    Please try to read what the error message is. The exact error message by the compiler I believe, is "Extension method must be defined in a non-generic static class". If you want to ask why does this restriction exist, please ask that. – Nikhil Girraj Feb 09 '17 at 11:22
  • 2
    Please create a [mcve] that can be compiled directly. Also always post exact error messages. – H H Feb 09 '17 at 11:33
  • @Chris Ah, okay, now I got it. You´re right with your edit and the generic type-param was actually ommited by the code-editor. Anyway the question is misleading as it states "I cannot declare an extension method in a non-generic static class" which you can. – MakePeaceGreatAgain Feb 09 '17 at 12:16
  • @HimBromBeere: Yeah, the question is still really misleading. That text is what I considered changing to make it accurate but that really would change the whole question (I'm not sure if the problem is just a misreading of can/cannot). – Chris Feb 09 '17 at 12:22

2 Answers2

2

After trying to understand your problem i think I understood it, first the error is wrong, the error on the compiler complains because an extension must be created in a non-generic static class, and the text you posted seems to say the contrary.

So, if the problem is that each T must have a different X, you need to store the class once created and reused then you can use a non-generic class with a dictionary:

public static class Extensions_S
{
    static Dictionary<Type, X> generators = new Dictionary<Type, X>();

    public static A generateA<T>(this T value)
    {
        X generator;

        if(!generators.TryGetValue(typeof(T), out generator)
        {
            generator = new X(typeof(T));
            generators.Add(typeof(T), generator);
        }

        return generator.from(value);
    }
}
Gusman
  • 14,905
  • 2
  • 34
  • 50
1
//public static class Extensions_S<T> { ...}
  public static class Extensions_S { ... }

Your generateA method can still be generic (like it already is) and you don't seem to use T anywhere else.

H H
  • 263,252
  • 30
  • 330
  • 514
  • OP obviously allready tried this: "Extensions_S is NOT allowed". Anyway this answer isn´t usefull at all for the community. MAybe it answers OPs question (which it doesn´t to seem), but without any further comments it´s not very meaningful. – MakePeaceGreatAgain Feb 09 '17 at 11:24
  • 2
    @HimBromBeere - "Extensions_S is NOT allowed" makes no sense at all. – H H Feb 09 '17 at 11:25
  • 2
    @HimBromBeere: That was a markup issue that I have fixed. In the source they had written `Extensions_S` but without code markup the `` was being lost. – Chris Feb 09 '17 at 11:26