Is it possible to import c# classes from dll in a vb.net dll?

10,603

Solution 1

I think you should rewrite your imports

Imports MyCSharpNamespace

without 'MyCSharpDll' part

Solution 2

You asked: "I created a c# dll project under Visual Studio 2010, with several public classes, and I would like to use its classes inside another dll project, but written in vb.net."

YES you can do it.

Just add to that DLL reference and that's it!

Solution 3

Assuming the dll in question is CLS compliant and compiled against the same runtime version, you should be able to use it without a problem.

If either (or both) of these conditions are not met, you will not be able to use the imported DLL.

Make sure the Import directive uses the namespace as defined in the assembly metadata - look at your C# project properties to see what the default namespace is, that's what you need to import.

Solution 4

You need to add a reference to your C# dll.

http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.80%29.aspx

Now you can instantiate a C# class from VB.NET:

Dim cClass = New MyCSharpNamespace.MyCSharpClass()
Share:
10,603

Related videos on Youtube

Alain
Author by

Alain

Updated on June 04, 2022

Comments

  • Alain
    Alain almost 2 years
    • IDE: VS2010
    • Framework.net: 4.0

    I created a c# dll project under Visual Studio 2010, with several public classes, and I would like to use its classes inside another dll project, but written in vb.net.

    In the vb.net dll project, I referenced the built c# dll, but impossible to import the c# classes. The namespace of the c# dll is even not recognized.

    What must I do to see my c# classes? If this is possible.

    Example of class of my c# dll (namespace MyCSharpDll):

    namespace MyCSharpNamespace {
      public class MyCSharpClass {
        public void Test() {}
      }
    }
    

    Example in a file of my vb.net dll:

    Imports MyCSharpDll.MyCSharpNamespace
    

    VS2010 indicates an error saying that MyCSharpDll is unknown or no public member.

    Thank you.

  • Alain
    Alain about 12 years
    I put the reference to the dll, as indicated in my message. Maybe in the c# project must I set something?
  • Oded
    Oded about 12 years
    Almost right. The imported DLL needs to be CLS compliant and compiled against the same runtime version in order to be imported successfully.
  • Alain
    Alain about 12 years
    Very good!! But I don't understand why I cannot write the full imports statement in my case. Note: At beginning, I tried this statement, but I didn't work, now yes.
  • Steve
    Steve about 12 years
    Because 'MyCSharpDll' isn't a namespace.
  • Alain
    Alain about 12 years
    it was not clear in my message but MyCSharpDll is the root namespace of the dll, sorry.
  • Alain
    Alain about 12 years
    Hi Dmitry, with the answer of @Steve, yes.
  • Alain
    Alain about 12 years
    @Oded, they were both CLS compliant and compiled against the same runtime version.
  • Alain
    Alain about 12 years
    The default namespace of my c# dll is "MyCSharpDll". See the answer of @Steve, but still I don't understand why I cannot import my c# classes with complete imports statement, ie MyCShrapDl.MCSharpNamespac,e like for my other imports.
  • Chris Dunaway
    Chris Dunaway about 12 years
    @Alain - C# doesn't have the concept of a "root" namespace like VB does. C# has a "default" namespace that is used in new files. Whatever is on the namespace statement is the namespace for the class.