...数组<对象 ^>^ args

发布于 2024-08-18 16:46:54 字数 1040 浏览 3 评论 0原文

我正在阅读 C++/CLI。我看到这个东西:

Object^ CreateInstanceFromTypename(String^ type, ...array<Object^>^ args)
{
if (!type)
throw gcnew ArgumentNullException("type");
Type^ t = Type::GetType(type);
if (!t)
throw gcnew ArgumentException("Invalid type name");
Object^ obj = Activator::CreateInstance(t, args);
return obj;
}

调用它时:

Object^ o = CreateInstanceFromTypename(
"System.Uri, System, Version=2.0.0.0, "
"Culture=neutral, PublicKeyToken=b77a5c561934e089",
"http://www.heege.net"
);

What is ...array^ args?如果我删除 ... ,则会出现编译错误:

error C2665: 'CreateInstanceFromTypeName' : none of the 2 overloads could convert all the argument types
1>        .\myFourthCPlus.cpp(12): could be 'System::Object ^CreateInstanceFromTypeName(System::String ^,cli::array<Type> ^)'
1>        with
1>        [
1>            Type=System::Object ^
1>        ]
1>        while trying to match the argument list '(const char [86], const char [21])'

I'm reading C++/CLI. I see this stuff:

Object^ CreateInstanceFromTypename(String^ type, ...array<Object^>^ args)
{
if (!type)
throw gcnew ArgumentNullException("type");
Type^ t = Type::GetType(type);
if (!t)
throw gcnew ArgumentException("Invalid type name");
Object^ obj = Activator::CreateInstance(t, args);
return obj;
}

When calling it:

Object^ o = CreateInstanceFromTypename(
"System.Uri, System, Version=2.0.0.0, "
"Culture=neutral, PublicKeyToken=b77a5c561934e089",
"http://www.heege.net"
);

What is ...array^ args? If I remove ... ,there's a complied-error:

error C2665: 'CreateInstanceFromTypeName' : none of the 2 overloads could convert all the argument types
1>        .\myFourthCPlus.cpp(12): could be 'System::Object ^CreateInstanceFromTypeName(System::String ^,cli::array<Type> ^)'
1>        with
1>        [
1>            Type=System::Object ^
1>        ]
1>        while trying to match the argument list '(const char [86], const char [21])'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-08-25 16:46:54

与 C++ 一样,C++/CLI 具有可变数量参数的机制。这就是 ...array^ 参数前面的 ... 的含义。

为了类型安全,C++/CLI 设计者添加了托管语法来声明变量数组的类型。

由于它只是将该参数传递给 Activator::CreateInstance() 函数,因此我将查看 Activator 函数正在寻找哪些变量参数。

Like C++, C++/CLI has a mechanism for a variable amount of arguments. That is what the ... in front of the ...array<Object^>^ parameter means.

For type safety the C++/CLI designers added managed syntax to declare the type of the variable array.

Since it's just passing that parameter to the Activator::CreateInstance() function, I would look at what variable parameters the Activator function is looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文