使用.NET中的语言访问具有区分大小写的名称的成员

时间:2022-10-16 07:20:34

I just came across an interesting scenario. I have a class in C#:

我刚刚遇到了一个有趣的场景。我有一个C#课:

public class Test
{
  public int A;
  public int a;
}

As C# is case sensitive, this will treat the two variables A and a as distinct. I want to inherit the above class in my VB code, which is not case sensitive. How will the VB code access the two distinct variables A and a?

由于C#区分大小写,因此将两个变量A和a视为不同。我想在我的VB代码中继承上面的类,它不区分大小写。 VB代码如何访问两个不同的变量A和a?

Any help is appreciated.

任何帮助表示赞赏。

4 个解决方案

#1


The common language specification (CLS) which ensures cross language compatibility tells you not to declare two public members that are only different in case. Such a code won't be CLS compliant.

确保跨语言兼容性的通用语言规范(CLS)告诉您不要声明两个仅在大小写上不同的公共成员。这样的代码不符合CLS。

If you can't change the code of the library, you can use reflection API to manually select the field you want:

如果您无法更改库的代码,则可以使用反射API手动选择所需的字段:

obj.GetType().GetField("a").GetValue(obj)
obj.GetType().GetField("A").GetValue(obj)

#2


Like Mehrdad said it is not CLS compliant to declare two public members that are only different in case

And if you want visual studio to help you write CLS complaint code that can be used from any other .NET languages , just write

就像Mehrdad所说的那样,声明两个只有不同的公共成员并不符合CLS。如果你想让visual studio帮助你编写可以在任何其他.NET语言中使用的CLS投诉代码,那就写一下

[assembly: System.CLSCompliant(true)]

in your AssemblyInfo.cs file, if you did something wrong after writing this line, visual studio will not be happy :)
EDIT: or AssemblyInfo.vb if you are using VB.NET, thanks Lucas

在你的AssemblyInfo.cs文件中,如果你在写完这一行后做错了什么,visual studio就不会高兴了:)编辑:或者汇编信息。如果你使用的是VB.NET,感谢Lucas

#3


for that you need to use reflection functionality

为此你需要使用反射功能

I saw how somewhere, I will try to find it

我看到了某个地方,我会试着找到它

here is the link to how

这是如何链接

in this solution above, it's about function, for a variable I think this would would be ok

在上面的这个解决方案中,它是关于函数,对于变量我认为这样就可以了

at least you get both now :-)

至少你现在两个都得到:-)

#4


If you intend for your code to be cross functional between languages, you will need to follow a naming standard that will work for both languages.

如果您希望代码在语言之间交叉使用,则需要遵循适用于这两种语言的命名标准。

I personally find it very complicated to have two public members that have the same name, just different capitalization.

我个人觉得有两个公共成员具有相同的名称,只是不同的大小写非常复杂。

#1


The common language specification (CLS) which ensures cross language compatibility tells you not to declare two public members that are only different in case. Such a code won't be CLS compliant.

确保跨语言兼容性的通用语言规范(CLS)告诉您不要声明两个仅在大小写上不同的公共成员。这样的代码不符合CLS。

If you can't change the code of the library, you can use reflection API to manually select the field you want:

如果您无法更改库的代码,则可以使用反射API手动选择所需的字段:

obj.GetType().GetField("a").GetValue(obj)
obj.GetType().GetField("A").GetValue(obj)

#2


Like Mehrdad said it is not CLS compliant to declare two public members that are only different in case

And if you want visual studio to help you write CLS complaint code that can be used from any other .NET languages , just write

就像Mehrdad所说的那样,声明两个只有不同的公共成员并不符合CLS。如果你想让visual studio帮助你编写可以在任何其他.NET语言中使用的CLS投诉代码,那就写一下

[assembly: System.CLSCompliant(true)]

in your AssemblyInfo.cs file, if you did something wrong after writing this line, visual studio will not be happy :)
EDIT: or AssemblyInfo.vb if you are using VB.NET, thanks Lucas

在你的AssemblyInfo.cs文件中,如果你在写完这一行后做错了什么,visual studio就不会高兴了:)编辑:或者汇编信息。如果你使用的是VB.NET,感谢Lucas

#3


for that you need to use reflection functionality

为此你需要使用反射功能

I saw how somewhere, I will try to find it

我看到了某个地方,我会试着找到它

here is the link to how

这是如何链接

in this solution above, it's about function, for a variable I think this would would be ok

在上面的这个解决方案中,它是关于函数,对于变量我认为这样就可以了

at least you get both now :-)

至少你现在两个都得到:-)

#4


If you intend for your code to be cross functional between languages, you will need to follow a naming standard that will work for both languages.

如果您希望代码在语言之间交叉使用,则需要遵循适用于这两种语言的命名标准。

I personally find it very complicated to have two public members that have the same name, just different capitalization.

我个人觉得有两个公共成员具有相同的名称,只是不同的大小写非常复杂。