如何将VB集合从c#代码传递给函数?

时间:2018-05-11 20:44:03

标签: c# asp.net

我有一个dll,它的函数需要几个Type.VisualBasic.Collection变量。但我尝试将它们作为An Arraylist和C#中的列表传递无效。

  Severity  Code    Description Project File    Line    Suppression State
  Error CS1502  The best overloaded method match for 
 'Object.RptOptimizer.BuildReport(string, string, System.Guid, 
 Microsoft.VisualBasic.Collection, Microsoft.VisualBasic.Collection, string, 
 string, System.DateTime, System.DateTime, string, string, string, string, 
 string, int, bool, int, int, bool)' has some invalid arguments EPWeb4   
 C:\inetpub\wwwroot\EPWeb4\Forms\RptParamsOptimizer.aspx.cs 271 Active

 Severity   Code    Description Project File    Line    Suppression State
 Error  CS0012  The type 'Collection' is defined in an assembly that is not 
 referenced. You must add a reference to assembly 'Microsoft.VisualBasic, 
 Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.    

1 个答案:

答案 0 :(得分:0)

Microsoft.VisualBasic.Collection实现ICollectionIList,就像许多标准BCL集合一样。

static void Main(string[] args)
{
    Microsoft.VisualBasic.Collection c = new Microsoft.VisualBasic.Collection();

    c.Add(new { Id = 1, Name = "John"});
    c.Add(new { Id = 2, Name = "Mary" });

    DoSomething(c);

    Console.ReadLine();
}

public static void DoSomething(ICollection collection)
{
    foreach (dynamic v in collection)
    {
        Console.WriteLine($"{v.Id} - {v.Name}");
    }
}

从上面的错误中,您还缺少C#项目中引用页面对Microsoft Visual Basic程序集的程序集引用。

右键单击Project>添加>参考...> “装配标签”>向下滚动到Microsoft.VisualBasic并勾选以包含它。