简体   繁体   English

C#程序集中的类对VB不可见

[英]Class from c# assembly not visible to VB

I have an assembly which was written in C#. 我有一个用C#编写的程序集。 Inside of it, I have a number of static classes that have static functions etc. Referencing these functions from another c# project works just fine, but if I try to use them in a VB.NET project, the classes are not available / visible at all, even though they appear in the object browser, and the project will compile / run as long as I don't reference said classes. 在它的内部,我有许多具有静态功能等的静态类。从另一个c#项目中引用这些功能也可以,但是如果我尝试在VB.NET项目中使用它们,则这些类在以下位置不可用/可见所有这些,即使它们出现在对象浏览器中,只要我不引用上述类,项目都将编译/运行。

Here is how I define the class in C#, for example... 例如,这是我在C#中定义类的方式。

namespace Tools
{
    public static class MyTools
    {
        public static int SumNumber(int n1, int n2) { return n1 + n2; }
    }
}

Attempting to use the class in VB will result in the error: 尝试在VB中使用该类将导致错误:

'MyTools' is not declared. It may be inaccessible due to its protection level.

Attempts to use a fully qualified name 'Tools.MyTools.SumNumber....' will result in the exact same error message. 尝试使用完全限定的名称“ Tools.MyTools.SumNumber ....”将导致完全相同的错误消息。

I have checked, both the C# assembly and the VB project are targeting .NET 4.5 What am I missing here? 我已经检查过,C#程序集和VB项目都以.NET 4.5为目标。

I cleared the 'Root Namespace' option in the project properties and now everything is working fine. 我清除了项目属性中的“ Root Namespace”选项,现在一切正常。 Leaving it in leads to non-obvious namespace nesting that confuses programmers. 保留它会导致非显而易见的名称空间嵌套,这会使程序员感到困惑。

This problem is typically due to not using the proper namespace when you try to use the class. 此问题通常是由于尝试使用该类时未使用正确的名称空间而引起的。 Get the full namespace from the C# code then use that namespace to fully qualify the class name when calling it on the VB side. 从C#代码获取完整的名称空间,然后在VB端调用该名称空间时使用该名称空间完全限定类名称。 If that works you should either import that namespace at the project or file level, or rework your namespaces for easier access. 如果可行,您应该在项目或文件级别导入该名称空间,或者重新设计您的名称空间以便于访问。

If that still doesn't work make sure you have a proper reference to the c# project from the VB project. 如果那仍然不起作用,请确保您对VB项目中的c#项目有正确的引用。 Make sure you didn't accidentally reference a copy of the output dll or some other stale file. 确保您没有意外引用输出dll或其他陈旧文件的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM