简体   繁体   English

在 C#.net 中扩展 VB.net 类

[英]Extending VB.net Classes in C#.net

I am currently exploring the possibility of combining both VB.net and C#.net into the same solution, with a view to stopping writing VB.net code and later down the line, refactoring what's been written into C#.我目前正在探索将 VB.net 和 C#.net 结合到同一个解决方案中的可能性,以期停止编写 VB.net 代码,然后再下线,重构已写入 C# 的内容。 My understanding of the .net framework suggests that I can extend a class of one language using a class of another language, but I simply cannot find a way to make it work.我对 .net 框架的理解表明我可以使用另一种语言的类来扩展一种语言的类,但我根本找不到让它工作的方法。

As a mock-up, I've created a VB.Net console app that looks a lot like this:作为一个模型,我创建了一个 VB.Net 控制台应用程序,它看起来很像这样:

Namespace CrossLanguage

Module Module1

    Sub Main()

        Console.WriteLine("This is a test")
        Console.ReadLine()

        Dim x As New Test

        x.WriteMeAnAnimal()

    End Sub

End Module

Public Class Test

    Sub WriteMeAnAnimal()

        Console.WriteLine("An animal")
        Console.ReadLine()

    End Sub

End Class

End Namespace

This works and there's no real issue with it.这有效并且没有真正的问题。 I can run up the app and at the first ReadLine, I can submit anything I like and 'An animal' appears.我可以运行应用程序,在第一个 ReadLine 处,我可以提交任何我喜欢的东西,然后会出现“动物”。 Now, all I've done next is create a new C# project, with one class file.现在,我接下来要做的就是创建一个新的 C# 项目,其中包含一个类文件。 I've added a project reference to point to the VB.net project (which is fine), but I simply cannot get the C# class to see that the VB class exists:我添加了一个项目引用来指向 VB.net 项目(这很好),但我根本无法让 C# 类看到 VB 类存在:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Test;

namespace CrossLanguage
{
    public class Dog : Test
    {
    }
}

The IDE errors on the references to Test, with the whole "could not be found, are you missing..." shenanigans.对 Test 的引用的 IDE 错误,以及整个“无法找到,您是否错过了……”恶作剧。

Am I missing something?我错过了什么吗?

It sadly seems that this was some quirk of Visual Studio.可悲的是,这似乎是 Visual Studio 的一些怪癖。 Having reviewed your comments, I sat down today and opened the solution to try a couple of things and, well, it works without me changing anything.在查看了您的评论后,我今天坐下来打开解决方案尝试了一些事情,嗯,它无需我更改任何内容即可工作。

Thanks for your help regardless.无论如何,感谢您的帮助。

Ashilta阿希尔塔

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

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