c++-cli - C++ CLI 通用函数
问题描述
我需要一个将 std::vector 转换为 CLI 列表的函数
generic<typename T> where T:CliCommonObjectBase
List<T>^ Converter::ConvertDataBaseListToList(DBList<TMObject> list)
{
List<T>^ returnList = gcnew List<T>();
for (DBIterator<TMObject> iter = list.first(); !iter.done(); iter.next())
{
DBRef<TMObject> tempObject = *iter;
returnList->Add(gcnew T("BlaBla"));
}
return returnList;
}
来自 CliCommonObjectBase 的构造函数
CliCommonObjectBase(String^ objectRefString);
通话
ConvertDataBaseListToList<CliMeeting^>(getReadBase()->getTermine());
CliMeeting 继承 CliCommonObjectBase
我的问题是 gcnew T("BlaBla") 给出错误
解决方案
我做了一个小例子,如何使用其中包含参数的构造函数来实例化泛型类型:
using namespace System;
ref struct A
{
A(String^ s)
{
Console::WriteLine(s);
}
};
generic<typename T> where T : A
T CreateObject()
{
auto args = gcnew array<Object^>(1);
args[0] = "BlaBla";
return static_cast<T>(Activator::CreateInstance(T::typeid, args));
}
推荐阅读
- php - 如何确定 php 中 PNG 图像的“每行字节数”?
- image-processing - scikit 图像(skimage)在梯度计算中排除白色背景
- python - 如何等到表格加载到网页上
- react-native - 无法使用 npm pack 生成的包解析类
- arm - PUSH 和 POP 括号中的寄存器排序
- node.js - Google Drive API 在获取文件时显示“禁止”
- google-drive-api - Google Drive API v2:生命终结?
- python - 'y.values == 0,1' 的计算结果是什么,y 是熊猫系列的 0 和 1?
- image - TVirtualStringTree 从 5.3 更新到 7.4 后显示双图
- amazon-web-services - STM32F4 和 AWS Kinesis 视频流