VB .NET project Compiles failed when use the Extension Function wrote by C++/CLI

Mage Luzdia 20 Reputation points
2023-05-17T19:24:37.5366667+00:00

I wrote a C++/CLI Assembly and use the [ExtensionAttribute],coding like this:

using namespace System::Runtime::CompilerServices;

namespace ExFunLibWithCli {
  public ref class ClassBase
  {
    public:
    int BaseFun()
    {
      return 32;
    }
  };

  [ExtensionAttribute]
  public ref class ClassBaseExtensions abstract sealed
  {
    public:
    [ExtensionAttribute]
    static int ExtensionFunc(ClassBase^ cb)
    {
      return 64;
    }
  };
}

In another C# .NET project,I add the Assembly generated by this code to be referenced and coding like:

static void Main(string[] args)
{

  var cb = new ClassBase();

  int res = cb.ExtensionFunc();

}

It compiles and works well!

Now I create another VB .NET project,and do the same thing:

Sub Main()
  Dim cb = New ClassBase
  Dim res = cb.ExtensionFunc()                     'this line compiles failed
  Dim res = ClassBaseExtensions.ExtensionFunc(cb)  'this line can compiles and work well,but I want to use ClassBase directly
End Sub

btw,When I use C# to write the class and functions decorated by [ExtensionAttribute],and use it in the same VB.NET project,it also compiles and work well.

So i'm really confused about it.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,572 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
326 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,612 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,642 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.1K Reputation points
    2023-05-17T19:50:32.3733333+00:00

    Try adding this line to your C++/CLI code (or to AssemblyInfo.cpp, which is usually present):

    [assembly:Extension];
    

    Then rebuild the projects.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful