简体   繁体   English

c#.net和vb.net中相同dll的属性差异

[英]Difference in the properties for the same dll in c#.net and vb.net

I am adding a dll for my project. 我正在为我的项目添加一个dll。 My project is in vb.net and dll is written in c#.net. 我的项目在vb.net中,dll是用c#.net编写的。 When I add this to a vb.net project the properties that are available are different (less in number) to the properties that are available if the same dll is added to ac# project. 当我将其添加到vb.net项目时,如果将相同的dll添加到ac #project,则可用的属性与可用的属性不同(数量较少)。

Object Browser view when added to a VB.Net Project 添加到VB.Net项目时的对象浏览器视图 在此输入图像描述

Object Browser view when added to ac# project 添加到ac #project时的对象浏览器视图 添加到c#项目时的对象浏览器视图

If you see in c# view you can access properties like "hits" and "facets" which are not accessible in vb.net. 如果您在c#视图中看到,您可以访问vb.net中无法访问的“hits”和“facets”等属性。

Can anyone please help me understand this issue. 任何人都可以帮我理解这个问题。

There are differences between VB.NET and C#. VB.NET和C#之间存在差异。 Make sure you compile your C# dll with the assembly attribute CLSCompliant set to true so your compiler can determine if all your publics are compatible to the other .NET languages. 确保使用程序集属性CLSCompliant设置为true来编译C#dll,以便编译器可以确定所有公共文件是否与其他.NET语言兼容。

To clarify my situation I am using a dll downloaded from github. 为了澄清我的情况,我使用从github下载的DLL。 When I downloaded the source code from github I found out that it is not CLSComplaint. 当我从github下载源代码时,我发现它不是CLSComplaint。 When I changed the property name and added DataMember attribute (for jsonserilzation) all the properties are available in vb.net project as well, but the problem I will be facing is merging of the code changes to any updates in the particular class library in original version to the CLSComplaint one which I don't want to commit to GitHub until I speak with author. 当我更改属性名称并添加DataMember属性(用于jsonserilzation)时,所有属性也可以在vb.net项目中使用,但我将面临的问题是将代码更改合并到原始特定类库中的任何更新版本到CLSComplaint,我不想提交给GitHub,直到我与作者交谈。

As a work around I added GetHitCount(CLSComplaint property name) 作为一个解决方法我添加了GetHitCount(CLSComplaint属性名称)

public int GetHitCount
{
    get { return hits.total;}
}

this exposes the "hits" property which is not available when the dll is added to vb.net project. 这暴露了将“dll”添加到vb.net项目时不可用的“命中”属性。

It's likely that "hits" and "facets" in the C# project have members in the class that have the same name, but different case. C#项目中的“命中”和“方面”可能在类中具有相同名称但不同情况的成员。 This is a very common issue for VB projects referencing C# dlls, and it also will result in the C# dll not being CLS Compliant. 对于引用C#dll的VB项目来说,这是一个非常常见的问题,它也会导致C#dll不符合CLS标准。 VB is case-insensitive, so it cannot determine which "hits" is being needed if there also is a "Hits" member - as a result VB will not display either member. VB不区分大小写,因此如果还有“Hits”成员,则无法确定需要哪些“命中” - 因此VB不会显示任何一个成员。

You can still call "hits" and "facets", but you will need to use "InvokeMember" to do this (using reflection). 您仍然可以调用“hits”和“facets”,但是您需要使用“InvokeMember”来执行此操作(使用反射)。

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

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