Domanda

I have checked, this works (I can call the extension method from a C# program):

[ExtensionAttribute]
public ref class ArrayExtensions abstract sealed {
public:        
    [ExtensionAttribute]
    static array<int>^ FlipExt(array<int> ^ vals)
    {
        int n = vals->Length;
        for (int i = 0; i < n / 2; ++i)
        {
            int tmp = vals[i];
            vals[i] = vals[n - i - 1];
            vals[n - i - 1] = tmp;
        }
        return vals;
    }
};

This also works (I can call the generic method from a C# program):

public ref class ArrayUtils abstract sealed {
public:        

    generic <typename T>
    static array<T>^ FlipGen(array<T> ^ vals)
    {
        int n = vals->Length;
        for (int i = 0; i < n / 2; ++i)
        {
            T tmp = vals[i];
            vals[i] = vals[n - i - 1];
            vals[n - i - 1] = tmp;
        }
        return vals;
    }
};

This C# code also works, so generic extension methods are supported by .NET:

public static class ArrayExtensionsSharp
{
    public static T[] FlipExtSharp<T>(this T[] vals)
    {
        int n = vals.Length;
        for (int i = 0; i < n / 2; ++i)
        {
            var tmp = vals[i];
            vals[i] = vals[n - i - 1];
            vals[n - i - 1] = tmp;
        }
        return vals;
    }
}

But when I do this, it says "error C2059: syntax error : 'generic'"

[ExtensionAttribute]
public ref class ArrayExtensions abstract sealed {
public:        
    [ExtensionAttribute]
    generic <typename T>
    static array<T>^ FlipGenExt(array<T> ^ vals)
    {
        int n = vals->Length;
        for (int i = 0; i < n / 2; ++i)
        {
            T tmp = vals[i];
            vals[i] = vals[n - i - 1];
            vals[n - i - 1] = tmp;
        }
        return vals;
    }
};

So, what's up? Is there a way to make a generic extension method in C++/CLI?

I want to do it in C++/CLI because the actual extension method I need is closely related to a C++/CLI method and I wan't them both in the same assembly. Thanks.

edit: Note that the answer below works, but the MSDN documentation seems (to me) to indicate otherwise:

generic class definition (from this page)

[attributes]
generic <class-key type-parameter-identifier(s)>
[constraint-clauses]
[accessibility-modifiers] ref class identifier  [modifiers]
[: base-list] 
{ 
class-body 
} [declarators] [;]

generic function definition (from this page)

[attributes] [modifiers]
return-type identifier <type-parameter identifier(s)>
[type-parameter-constraints clauses]

([formal-parameters])
{
   function-body
}

It looks like the generic function specification is for C#, not C__ (no "generic" keyword, comes right after the function name.) But it is on the C++ page, I guess I looked at the class specification, which has attributes before the word generic, but this is wrong too. Are the Microsoft docs just totally wrong here?

È stato utile?

Soluzione

Just change the placement of the attribute:

 generic <typename T>
 [ExtensionAttribute]
 static array<T>^ FlipGenExt(array<T> ^ vals)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top