简体   繁体   English

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

[英]How do you pass a VB collection to a function from c# code?

I have a dll that has a function which requires a couple variables of Type Microsoft.VisualBasic.Collection. 我有一个具有功能的dll,该功能需要几个Microsoft.VisualBasic.Collection类型的变量。 But I tried passing them as An Arraylist and A List in C# to no avail. 但是我尝试将它们作为C#中的Arraylist和A List传递给我没有用。

  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'.    

The Microsoft.VisualBasic.Collection implements ICollection and IList just like a number of standard BCL collections do. 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}");
    }
}

From the error above, you're also missing an assembly reference to the Microsoft Visual Basic assembly from the references page in your C# Project. 由于上述错误,您还从C#项目的“引用”页面中缺少对Microsoft Visual Basic程序集的程序集引用。

Right click the Project > Add > Reference ... > Assemblies Tab > Scroll down to Microsoft.VisualBasic and tick to include it. 右键单击项目>添加>引用...>程序集选项卡>向下滚动到Microsoft.VisualBasic然后勾选以将其包括在内。

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

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