在C#中使用vb.net类vb、net

2023-09-05 04:05:17 作者:夜幕下淡淡的傻笑

如何创建在vb.net类型和使用C#中的类型? 我想我需要编译VB项目并添加引用的DLL,但不知道如果这是正确的方式做到这一点,或参考将是有效的。 这样做是不是为了好玩,我们有一些VB code和我们正在考虑这个选项。

How can I create a type in vb.net and use the type in c#? I guess I need to compile the vb project and add reference to the dll but have no idea if this is the right way to do it, or the reference will be valid. Doing this is not for fun, we have some vb code and we're considering this option.

推荐答案

只要创建在VB一个类库项目,然后从C#项目引用添加到该项目。 (或者,如果它是在一个不同的解决方案,添加一个引用到由VB项目创建的DLL)。它应该是绝对没问题 - 你将有机会获得所有的公开类型从你的类库。

Just create a Class Library project in VB, and then add a reference to that project from your C# project. (Or if it's in a different solution, add a reference to the DLL created by the VB project.) It should be absolutely fine - you'll have access to all the Public types from your class library.

要解决你的问题的意见,这些项目的可以的默认值都是同一个命名空间,但它通常是一个好主意,使每一个项目都有自己的命名空间,所以很明显,其中任何特定类型得来的。

To address the comment in your question, the projects can both default to the same namespace, but it's generally a better idea to make each project have its own namespace, so it's clear where any particular type comes from.