... array <object ^> ^ args </object ^>

时间:2010-01-20 15:00:08

标签: c++-cli

我正在阅读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"
);

什么是... 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])'

1 个答案:

答案 0 :(得分:0)

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

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

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