【问题标题】:Use vb.net module in c# project在 c# 项目中使用 vb.net 模块
【发布时间】:2015-03-07 03:11:13
【问题描述】:

我想在我的 c# 项目中使用 vb.net 模块。我编译了 vb.net 模块,并在 c# 项目中添加了对其 dll 的引用,但在 c# 项目中看不到它。该模块是公共的,我尝试在模块名称之前写它的命名空间(dll文件名),我尝试手动插入using moduleNameSpace,我检查了两个项目具有相同的目标框架(4.5没有客户端配置文件),但我仍然看不到它。我不知道我应该进一步做什么。 谁能帮我?谢谢!

vb.net 模块的一部分:

Namespace GestioneSetupFile
Public Module GestioneSetupFiles
   Private Const TAB_POS As Short = 30
   Dim Testo, Paragrafo As String
   Dim PtrParagrafo As Integer

   Public DEFAULT_APP_DIR As String = My.Application.Info.DirectoryPath
   'Public DEFAULT_APP_DIR As String = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\BTSR\PC-LINK NEMO"

    Public DEFAULT_CONFIG_DIR As String = DEFAULT_APP_DIR + "\Config"

    Public Sub CheckConfigDir()
    'Verifica la presenza della cartella Config
    'Se non esiste la crea e per compatibilità con le versioni precedenti ci copia dentro tutti i files .cfg e .dat che trova
    If Not My.Computer.FileSystem.DirectoryExists(DEFAULT_CONFIG_DIR) Then
        'Dim NomeFile As String
        My.Computer.FileSystem.CreateDirectory(DEFAULT_CONFIG_DIR)
        'NomeFile = Dir(DEFAULT_APP_DIR + "\*.cfg")
        'While NomeFile <> ""
        '    'My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    'per compatibilità con vecchio (se reinstallato) non sposta ma copia i file
        '    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    NomeFile = Dir()
        'End While
        'NomeFile = Dir(DEFAULT_APP_DIR + "\*.dat")
        'While NomeFile <> ""
        '    'My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    'per compatibilità con vecchio (se reinstallato) non sposta ma copia i file
        '    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\" + NomeFile, DEFAULT_CONFIG_DIR + "\" + NomeFile)
        '    NomeFile = Dir()
        'End While
    End If
End Sub
End Module
End Namespace

这是我在 c# 项目中的调用:

using System;
/*Others using*/
using Microsoft.VisualBasic;
using GestioneSetupFile; //Compile error

namespace UltraFeederControl
{
   public partial class Form1 : Form
   {
       private void updateConfigFile()
       {
           GestioneSetupFiles.CheckConfigDir();
       }
   }
}

【问题讨论】:

  • 为什么你不能从 vb.Module 中获取代码并将其转换为它的 C# 等效项并在 C# 或扩展方法中创建一个模拟相同功能的类.. 如果你不确定如何要做到这一点..然后发布模块代码签名,它的功能和使用之一可以为您指明转换代码的正确方向..
  • 如果没有对您的 VB 模块的(最小)定义、您在 C# 中的尝试使用以及您收到的编译器错误,没有人能够给您直接的答案。跨度>
  • @MethodMan 引用用 VB.NET 编写的 .net 程序集应该可以在 C# 中正常工作,而无需修改 C# 项目。一旦编译了 VB.NET(或 C#)源代码,任何 .NET 项目都应该能够引用它。请记住运行时版本等内容。
  • @MethodMan 是的,我正在做,即使我认为没有必要添加Microsoft.VisualBasic
  • 我永远不会将我的命名空间命名为接近于方法名称的方法名称,这太令人困惑,并为您目前看到的 Vitto 等问题打开了大门

标签: c# vb.net reference module include


【解决方案1】:

这里是您需要的 C# 转换代码。确保将其添加到您放置的类中

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

namespace GestioneSetupFile
{
    public static class GestioneSetupFiles
    {
        private const short TAB_POS = 30;
        static string Testo;
        static string Paragrafo;

        static int PtrParagrafo;
        public static string DEFAULT_APP_DIR = My.Application.Info.DirectoryPath;

        public static string DEFAULT_APP_DIR = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\BTSR\\PC-LINK NEMO";

        public static string DEFAULT_CONFIG_DIR = DEFAULT_APP_DIR + "\\Config";
        public static void CheckConfigDir()
        {

            if (!My.Computer.FileSystem.DirectoryExists(DEFAULT_CONFIG_DIR)) 
            {
                string NomeFile = null;
                My.Computer.FileSystem.CreateDirectory(DEFAULT_CONFIG_DIR);
                NomeFile = FileSystem.Dir(DEFAULT_APP_DIR + "\\*.cfg");
                while (!string.IsNullOrEmpty(NomeFile)) 
                {
                    My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    NomeFile = FileSystem.Dir();
                }
                NomeFile = FileSystem.Dir(DEFAULT_APP_DIR + "\\*.dat");
                while (!string.IsNullOrEmpty(NomeFile)) 
                {
                    My.Computer.FileSystem.MoveFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    My.Computer.FileSystem.CopyFile(DEFAULT_APP_DIR + "\\" + NomeFile, DEFAULT_CONFIG_DIR + "\\" + NomeFile);
                    NomeFile = FileSystem.Dir();
                }
            }
        }
    }
}

To see Hot Root NameSpace Work in VB vs C#.NET 看看这里,你会看到如何解决你的问题,否则转换后的部分就是我为你开始的部分在这里..

【讨论】:

  • 谢谢,但我只发布了模块的一部分,但是我想了解为什么我不能简单地包含它
  • 也许你需要完全限定命名空间我很想看看 C# 项目的命名空间是什么样子的。然后将你的 Visualbasic 命名为相同或访问它你需要这样做,但它是完全限定的命名空间这样做有意义吗
  • Vito 你也可以看看这个链接VB.NET Namespave
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 2019-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多