#include <iostream>
template <class T> struct TYPE;
template <class T> TYPE<T> foo(TYPE<T> &, TYPE<T> &);
template <class T> TYPE<T> bar(TYPE<T> &, TYPE<T> &);
template <class T>
struct TYPE
{
friend TYPE<T> foo<> ( TYPE<T> &, TYPE<T> & );
friend TYPE<T> bar<> ( TYPE<T> &, TYPE<T> & );
};
template <class T> TYPE<T> foo(TYPE<T> &, TYPE<T> &) { TYPE<T> res; std::cout << "1" << std::endl; return res; }
template <class T> TYPE<T> bar(TYPE<T> &, TYPE<T> &) { TYPE<T> res; std::cout << "2" << std::endl; return res; }
struct CLASS
{
typedef TYPE<int> (*ptr_to_function) ( TYPE<int> &, TYPE<int> & );
ptr_to_function ptr_to_foo = foo<int>; // Initialise pointer to foo
ptr_to_function ptr_to_bar = bar<int>;
void calculate () {
procedure ( ptr_to_bar );
procedure ( ptr_to_foo );
}
void procedure ( ptr_to_function fun )
{
TYPE<int> x;
fun(x, x);
}
};
int main()
{
CLASS c;
c.calculate();
}
该功能正常运行,但不适用于New-ItemProperty:
控制台输出:
Type paths: "D:\", "D:\1", "D:\2" New-ItemProperty : Cannot convert "System.String[]" to the type "System.String" required bt parameter "Name". Specified metod is not supported. line:12 char:83 + ... KCU:\Software\Microsoft\DirectX\UserGpuPreferences -Name $app -Proper ... + ~~~~ + CategoryInfo : InvalidArgument: (:) [New-ItemProperty], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewItemPropertyCommand
如果我想将路径设置为参数(用逗号分隔并用引号引起来),应该如何修改该功能?
答案 0 :(得分:2)
在将用户输入 传递到函数之前先对其进行清理/准备,然后使用循环处理函数参数(因为New-ItemProperty
需要一个字符串,而不是字符串数组)。< / p>
function Write-GpuPreference {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string[]]$Preference
)
Begin {
$path = 'HKCU:\Software\Microsoft\DirectX\UserGpuPreferences'
$value = 'GpuPreference=2;'
}
Process {
$Preference | Select-Object -Unique | ForEach-Object {
New-ItemProperty -Path $path -Name $_ -PropertyType String -Value $value -Force
}
}
}
$app = Read-Host -Prompt " "
$app = $app.Replace('"', '').Split(',').Trim()
Write-GpuPreference $app