如何:公开程序集中的 STL/CLR 容器

STL/CLR 容器(例如 listmap)作为模板 ref 类实现。 由于 C++ 模板在编译时实例化,因此具有相同签名但在不同程序集中的两个模板类实际上是不同的类型。 这意味着模板类不能跨程序集边界使用。

为了实现跨程序集共享,STL/CLR 容器实现泛型接口 ICollection<T>。 通过使用此泛型接口,所有支持泛型的语言(包括 C++、C# 和 Visual Basic)都可以访问 STL/CLR 容器。

本主题演示如何显示在名为 StlClrClassLibrary 的 C++ 程序集中编写的多个 STL/CLR 容器的元素。 我们将展示两个要访问 StlClrClassLibrary 的程序集。 第一个程序集是用 C++ 编写的,第二个是用 C# 编写的。

如果两个程序集都是用 C++ 编写的,则可以使用其 generic_container typedef 访问容器的泛型接口。 例如,如果你有一个 cliext::vector<int> 类型的容器,则其泛型接口为:cliext::vector<int>::generic_container。 同样,可以使用 generic_iterator typedef 通过泛型接口获取迭代器,如 cliext::vector<int>::generic_iterator 中所示。

由于这些 typedef 是在 C++ 头文件中声明的,所以用其他语言编写的程序集无法使用它们。 因此,若要以 C# 或任何其他 .NET 语言访问 cliext::vector<int> 的泛型接口,请使用 System.Collections.Generic.ICollection<int>。 若要循环访问此集合,请使用 foreach 循环。

下表列出了每个 STL/CLR 容器实现的泛型接口:

STL/CLR 容器 泛型接口
deque<T> ICollection<T>
hash_map<K, V> IDictionary<K, V>
hash_multimap<K, V> IDictionary<K, V>
hash_multiset<T> ICollection<T>
hash_set<T> ICollection<T>
list<T> ICollection<T>
map<K, V> IDictionary<K, V>
multimap<K, V> IDictionary<K, V>
multiset<T> ICollection<T>
set<T> ICollection<T>
vector<T> ICollection<T>

注意

由于 queuepriority_queuestack 容器不支持迭代器,因此它们不实现泛型接口,并且无法跨程序集访问。

示例 1

说明

在此示例中,我们声明包含专用 STL/CLR 成员数据的 C++ 类。 然后,声明公共方法以授予对类的专用集合的访问权限。 我们以两种不同的方式来执行此操作,一种用于 C++ 客户端,另一种用于其他 .NET 客户端。

代码

// StlClrClassLibrary.h
#pragma once

#include <cliext/deque>
#include <cliext/list>
#include <cliext/map>
#include <cliext/set>
#include <cliext/stack>
#include <cliext/vector>

using namespace System;
using namespace System::Collections::Generic;
using namespace cliext;

namespace StlClrClassLibrary {

    public ref class StlClrClass
    {
    public:
        StlClrClass();

        // These methods can be called by a C++ class
        // in another assembly to get access to the
        // private STL/CLR types defined below.
        deque<wchar_t>::generic_container ^GetDequeCpp();
        list<float>::generic_container ^GetListCpp();
        map<int, String ^>::generic_container ^GetMapCpp();
        set<double>::generic_container ^GetSetCpp();
        vector<int>::generic_container ^GetVectorCpp();

        // These methods can be called by a non-C++ class
        // in another assembly to get access to the
        // private STL/CLR types defined below.
        ICollection<wchar_t> ^GetDequeCs();
        ICollection<float> ^GetListCs();
        IDictionary<int, String ^> ^GetMapCs();
        ICollection<double> ^GetSetCs();
        ICollection<int> ^GetVectorCs();

    private:
        deque<wchar_t> ^aDeque;
        list<float> ^aList;
        map<int, String ^> ^aMap;
        set<double> ^aSet;
        vector<int> ^aVector;
    };
}

示例 2

说明

在此示例中,我们将实现示例 1 中声明的类。 为了使客户端使用此类库,我们使用清单工具 mt.exe 将清单文件嵌入 DLL 中。 有关详细信息,请参阅代码注释。

有关清单工具和并行程序集的详细信息,请参阅生成 C/C++ 独立应用程序和并行程序集

代码

// StlClrClassLibrary.cpp
// compile with: /clr /LD /link /manifest
// post-build command: (attrib -r StlClrClassLibrary.dll & mt /manifest StlClrClassLibrary.dll.manifest /outputresource:StlClrClassLibrary.dll;#2 & attrib +r StlClrClassLibrary.dll)

#include "StlClrClassLibrary.h"

namespace StlClrClassLibrary
{
    StlClrClass::StlClrClass()
    {
        aDeque = gcnew deque<wchar_t>();
        aDeque->push_back(L'a');
        aDeque->push_back(L'b');

        aList = gcnew list<float>();
        aList->push_back(3.14159f);
        aList->push_back(2.71828f);

        aMap = gcnew map<int, String ^>();
        aMap[0] = "Hello";
        aMap[1] = "World";

        aSet = gcnew set<double>();
        aSet->insert(3.14159);
        aSet->insert(2.71828);

        aVector = gcnew vector<int>();
        aVector->push_back(10);
        aVector->push_back(20);
    }

    deque<wchar_t>::generic_container ^StlClrClass::GetDequeCpp()
    {
        return aDeque;
    }

    list<float>::generic_container ^StlClrClass::GetListCpp()
    {
        return aList;
    }

    map<int, String ^>::generic_container ^StlClrClass::GetMapCpp()
    {
        return aMap;
    }

    set<double>::generic_container ^StlClrClass::GetSetCpp()
    {
        return aSet;
    }

    vector<int>::generic_container ^StlClrClass::GetVectorCpp()
    {
        return aVector;
    }

    ICollection<wchar_t> ^StlClrClass::GetDequeCs()
    {
        return aDeque;
    }

    ICollection<float> ^StlClrClass::GetListCs()
    {
        return aList;
    }

    IDictionary<int, String ^> ^StlClrClass::GetMapCs()
    {
        return aMap;
    }

    ICollection<double> ^StlClrClass::GetSetCs()
    {
        return aSet;
    }

    ICollection<int> ^StlClrClass::GetVectorCs()
    {
        return aVector;
    }
}

示例 3

说明

在此示例中,我们将创建一个 C++ 客户端,该客户端使用示例 1 和 2 中创建的类库。 此客户端使用 STL/CLR 容器的 generic_container typedef 来循环访问容器并显示其内容。

代码

// CppConsoleApp.cpp
// compile with: /clr /FUStlClrClassLibrary.dll

#include <cliext/deque>
#include <cliext/list>
#include <cliext/map>
#include <cliext/set>
#include <cliext/vector>

using namespace System;
using namespace StlClrClassLibrary;
using namespace cliext;

int main(array<System::String ^> ^args)
{
    StlClrClass theClass;

    Console::WriteLine("cliext::deque contents:");
    deque<wchar_t>::generic_container ^aDeque = theClass.GetDequeCpp();
    for each (wchar_t wc in aDeque)
    {
        Console::WriteLine(wc);
    }
    Console::WriteLine();

    Console::WriteLine("cliext::list contents:");
    list<float>::generic_container ^aList = theClass.GetListCpp();
    for each (float f in aList)
    {
        Console::WriteLine(f);
    }
    Console::WriteLine();

    Console::WriteLine("cliext::map contents:");
    map<int, String ^>::generic_container ^aMap = theClass.GetMapCpp();
    for each (map<int, String ^>::value_type rp in aMap)
    {
        Console::WriteLine("{0} {1}", rp->first, rp->second);
    }
    Console::WriteLine();

    Console::WriteLine("cliext::set contents:");
    set<double>::generic_container ^aSet = theClass.GetSetCpp();
    for each (double d in aSet)
    {
        Console::WriteLine(d);
    }
    Console::WriteLine();

    Console::WriteLine("cliext::vector contents:");
    vector<int>::generic_container ^aVector = theClass.GetVectorCpp();
    for each (int i in aVector)
    {
        Console::WriteLine(i);
    }
    Console::WriteLine();

    return 0;
}

输出

cliext::deque contents:
a
b

cliext::list contents:
3.14159
2.71828

cliext::map contents:
0 Hello
1 World

cliext::set contents:
2.71828
3.14159

cliext::vector contents:
10
20

示例 4

说明

在此示例中,我们将创建一个 C# 客户端,该客户端使用示例 1 和 2 中创建的类库。 此客户端使用 STL/CLR 容器的 ICollection<T> 方法来循环访问容器并显示其内容。

代码

// CsConsoleApp.cs
// compile with: /r:Microsoft.VisualC.STLCLR.dll /r:StlClrClassLibrary.dll /r:System.dll

using System;
using System.Collections.Generic;
using StlClrClassLibrary;
using cliext;

namespace CsConsoleApp
{
    class Program
    {
        static int Main(string[] args)
        {
            StlClrClass theClass = new StlClrClass();

            Console.WriteLine("cliext::deque contents:");
            ICollection<char> iCollChar = theClass.GetDequeCs();
            foreach (char c in iCollChar)
            {
                Console.WriteLine(c);
            }
            Console.WriteLine();

            Console.WriteLine("cliext::list contents:");
            ICollection<float> iCollFloat = theClass.GetListCs();
            foreach (float f in iCollFloat)
            {
                Console.WriteLine(f);
            }
            Console.WriteLine();

            Console.WriteLine("cliext::map contents:");
            IDictionary<int, string> iDict = theClass.GetMapCs();
            foreach (KeyValuePair<int, string> kvp in iDict)
            {
                Console.WriteLine("{0} {1}", kvp.Key, kvp.Value);
            }
            Console.WriteLine();

            Console.WriteLine("cliext::set contents:");
            ICollection<double> iCollDouble = theClass.GetSetCs();
            foreach (double d in iCollDouble)
            {
                Console.WriteLine(d);
            }
            Console.WriteLine();

            Console.WriteLine("cliext::vector contents:");
            ICollection<int> iCollInt = theClass.GetVectorCs();
            foreach (int i in iCollInt)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine();

            return 0;
        }
    }
}

输出

cliext::deque contents:
a
b

cliext::list contents:
3.14159
2.71828

cliext::map contents:
0 Hello
1 World

cliext::set contents:
2.71828
3.14159

cliext::vector contents:
10
20

另请参阅

STL/CLR 库参考