vb.net中不明确的类成员

时间:2023-01-15 16:51:38

I am trying to use a class from a C# assembly in vb.net. The class has ambiguous members because vb.net is case insensitive. The class is something like this:

我试图在vb.net中使用C#程序集中的类。该类具有不明确的成员,因为vb.net不区分大小写。这个类是这样的:

public class Foo {

  public enum FORMAT {ONE, TWO, THREE};

  public FORMAT Format {
    get {...}
    set {...}
   }
}

I try to access the enum: Foo.FORMAT.ONE

我尝试访问枚举:Foo.FORMAT.ONE

This is not possible because there is also a property named 'format'.

这是不可能的,因为还有一个名为'format'的属性。

I can not change the C# assembly. How can I get around this and reference the enum from vb.net?

我无法改变C#程序集。我怎样才能解决这个问题并从vb.net引用枚举?

2 个解决方案

#1


7  

I don't think you can get around this. Get in touch with the author of the C# component you are trying to use and convince them to fix their code.

我认为你不能解决这个问题。与您尝试使用的C#组件的作者联系,并说服他们修复他们的代码。

Incidentally, this is the primary reason behind the CLSCompliant(true) attribute, which if you are writing APIs or other code that has a high probability of being used by other languages you should always set. It would have flagged this issue for the original author to be aware of and fix correctly.

顺便说一句,这是CLSCompliant(true)属性背后的主要原因,如果您正在编写API或其他语言很有可能被其他语言使用,则应始终设置该属性。它会标记此问题,以便原作者能够正确地了解和修复。

#2


4  

There are a couple of ways you can work around it, but neither one is really a good option.

有几种方法可以解决它,但没有一种方法真的是一个不错的选择。

One is to create a C# project and completely wrap the class, changing the ambiguous members into unambiguous ones. Depending on how big the class is, it could be a lot of work, though you only have to wrap the members you need, obviously.

一种是创建一个C#项目并完全包装该类,将模糊成员更改为明确的成员。根据课程的大小,它可能会有很多工作,但显然你只需要包装你需要的成员。

The other is to use reflection, which isn't as much work as wrapping, but is still pointless work compared to the author just writing the code correctly in the first place.

另一种方法是使用反射,这不像包装那么多,但与作者首先正确编写代码相比,仍然是毫无意义的工作。

#1


7  

I don't think you can get around this. Get in touch with the author of the C# component you are trying to use and convince them to fix their code.

我认为你不能解决这个问题。与您尝试使用的C#组件的作者联系,并说服他们修复他们的代码。

Incidentally, this is the primary reason behind the CLSCompliant(true) attribute, which if you are writing APIs or other code that has a high probability of being used by other languages you should always set. It would have flagged this issue for the original author to be aware of and fix correctly.

顺便说一句,这是CLSCompliant(true)属性背后的主要原因,如果您正在编写API或其他语言很有可能被其他语言使用,则应始终设置该属性。它会标记此问题,以便原作者能够正确地了解和修复。

#2


4  

There are a couple of ways you can work around it, but neither one is really a good option.

有几种方法可以解决它,但没有一种方法真的是一个不错的选择。

One is to create a C# project and completely wrap the class, changing the ambiguous members into unambiguous ones. Depending on how big the class is, it could be a lot of work, though you only have to wrap the members you need, obviously.

一种是创建一个C#项目并完全包装该类,将模糊成员更改为明确的成员。根据课程的大小,它可能会有很多工作,但显然你只需要包装你需要的成员。

The other is to use reflection, which isn't as much work as wrapping, but is still pointless work compared to the author just writing the code correctly in the first place.

另一种方法是使用反射,这不像包装那么多,但与作者首先正确编写代码相比,仍然是毫无意义的工作。