What is equivalent to Microsoft.VisualBasic.Collection in c#?
I have a method which takes a stored procedure name and a Microsoft.VisualBasic.Collection? I am referencing a vb project in which I have to pass in a collecti开发者_如何学Pythonon to this method, but the current project I am in is in c#, so I am unclear what I can pass into the method?
Here is the vb call:
public void CallStoredProc(string spName, Microsoft.VisualBasic.Collection params);
In my c# app, I need to call this an pass in the appropriate c# object to params.
One option is to simply use the Collection
type directly from C#. This is a standard type in the Microsoft.VisualBasic.dll
assembly and can be used from any .Net language.
The closest collection in the standard BCL though is Hashtable
. Converting between Hashtable
and Collection
should be fairly straight forward
For example
using VBCollection = Microsoft.VisualBasic.Collection;
...
public static VBCollection ToVBCollection(this Hashtable table) {
var collection = new VBCollection();
foreach (var pair in table) {
// Note: The Add method in collection takes value then key.
collection.Add(pair.Value, pair.Key);
}
return collection;
}
Note: This is not a direct mapping though. The Collection
type supports a number of operations which Hashtable
does not like: before and after values, index by number or key, etc ... My approach would be to use one type consistently throughout the application and change either the C# or VB project appropriately
Unless you can change the method, so that it takes ICollection
, IEnumerable
or their generic variants, you have to pass an instance of Microsoft.VisualBasic.Collection
to that method.
From the point of view of C#, Microsoft.VisualBasic.Collection
is just a class and you can work with it as with any other class, i.e. instance it:
new Microsoft.VisualBasic.Collection()
Of course, you have to reference the assembly Microsoft.VisualBasic.dll in your project.
There is no such thing as a "C# object", or a "VisualBasic.net object" - it is all .net, so you can simply include a reference to Microsoft.VisualBasic.dll and use that Microsoft.VisualBasic.Collection.
C# devs often frown upon Microsoft.VisualBasic.dll because of the name, but you won't be eaten by Velociraptors if you use it since .net is properly and fully language-independent.
精彩评论