PowerShell中的泛型

Rie*_*ies 4 generics powershell sharepoint

我需要在PowerShell脚本中以下列方式使用SharePoint API:

C#代码:

var service = farm.Services.GetValue<SPWebService>();
Run Code Online (Sandbox Code Playgroud)

如何在PowerShell中指定泛型参数?

目前我得到一个Exception,声明"无法对ContainsGenereicParameters为true的类型或方法执行后期绑定操作"

Mar*_*ann 7

这不是你可以轻易做到的事情.你将不得不求助于反思来完成这项工作.有关更多信息,请参阅此博客文章.

  • 谢谢你的链接.我能够提出以下解决方案:[System.Reflection.Assembly] :: LoadWithPartialName("Microsoft.SharePoint")$ farm = [Microsoft.SharePoint.Administration.SPFarm] :: Local $ method = [Microsoft.SharePoint .Administration.SPServiceCollection] .GetMethod("GetValue",[string])$ closedmethod = $ method.MakeGenericMethod([Microsoft.SharePoint.Administration.SPWebService])$ service = $ closedmethod.Invoke($ farm.Services,"") (3认同)