如何在PowerShell中执行lambda表达式? [英] How do I do a lambda expression in PowerShell?

查看:67
本文介绍了如何在PowerShell中执行lambda表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下在C#中正常工作(使用Microsoft.SharePoint.Client):

The following works fine in C# (using Microsoft.SharePoint.Client):

ClientContext sourceCtx = new ClientContext(sourceSiteUrl);
sourceCtx.Credentials = new NetworkCredential("username", "password");
Site sourceSite = sourceCtx.Site;
sourceCtx.Load(sourceSite, s => s.Usage);
sourceCtx.ExecuteQuery();

但是,我真的希望在PowerShell中运行它.我不知道的部分是lambda表达式s => s.Usage.看着这个问题,我发现答案可能是内联脚本块,所以我尝试了以下:

However, I really want this working in PowerShell. The part that I can't figure out is the lambda expression s => s.Usage. Looking at this question I see that the answer may be to have an inline scriptblock, so I tried the following:

$lambda = {
    param($s)
    $s.Usage
}
$sourceCtx.Load($sourceSite, $lambda)
$sourceCtx.ExecuteQuery();

但是在Load方法中,以下操作失败:

But that fails with the following on the Load method:

找不到"load"的重载,并且参数计数为"2".

Cannot find an overload for "load" and the argument count: "2".

基本上,我需要将某种类型的类型转换为:

Basically, I need to do some sort of type conversion into:

Expression<Func<T, Object>>[]

有什么想法吗?

推荐答案

由于我的无知,我认为那还不错. 不会很有趣,但这似乎是唯一的例子

In my ignorance I thought it wouldn't be bad.... It's not going to be fun but this seems to be the only example

http ://www.itunity.com/article/loading-specific-values-lambda-expressions-sharepoint-csom-api-windows-powershell-1249

C:\Scripts\Load-CSOMProperties.ps1 
$web = $ctx.Web 
Load-CSOMProperties -object $web -propertyNames @("AllProperties", "Url", "Title") 
$ctx.ExecuteQuery() 

$web = $ctx.Web 
Load-CSOMProperties -parentObject $web -collectionObject $web.Fields -propertyNames @("Id", "InternalName") -parentPropertyName "Fields" 
$ctx.ExecuteQuery()

<#
.SYNOPSIS
Facilitates the loading of specific properties of a Microsoft.SharePoint.Client.ClientObject object or Microsoft.SharePoint.Client.ClientObjectCollection object.

.DESCRIPTION
Replicates what you would do with a lambda expression in C#. 
For example, "ctx.Load(list, l => list.Title, l => list.Id)" becomes
"Load-CSOMProperties -object $list -propertyNames @('Title', 'Id')".

.EXAMPLE
Load-CSOMProperties -parentObject $web -collectionObject $web.Fields -propertyNames @("InternalName", "Id") -parentPropertyName "Fields" -executeQuery
$web.Fields | select InternalName, Id

.EXAMPLE
Load-CSOMProperties -object $web -propertyNames @("Title", "Url", "AllProperties") -executeQuery
$web | select Title, Url, AllProperties
#>

function global:Load-CSOMProperties {
[CmdletBinding(DefaultParameterSetName='ClientObject')]
param (
    # The Microsoft.SharePoint.Client.ClientObject to populate.
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "ClientObject")]
    [Microsoft.SharePoint.Client.ClientObject]
    $object,

    # The Microsoft.SharePoint.Client.ClientObject that contains the collection object.
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "ClientObjectCollection")]
    [Microsoft.SharePoint.Client.ClientObject]
    $parentObject,

    # The Microsoft.SharePoint.Client.ClientObjectCollection to populate.
    [Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 1, ParameterSetName = "ClientObjectCollection")]
    [Microsoft.SharePoint.Client.ClientObjectCollection]
    $collectionObject,

    # The object properties to populate
    [Parameter(Mandatory = $true, Position = 1, ParameterSetName = "ClientObject")]
    [Parameter(Mandatory = $true, Position = 2, ParameterSetName = "ClientObjectCollection")]
    [string[]]
    $propertyNames,

    # The parent object's property name corresponding to the collection object to retrieve (this is required to build the correct lamda expression).
    [Parameter(Mandatory = $true, Position = 3, ParameterSetName = "ClientObjectCollection")]
    [string]
    $parentPropertyName,

    # If specified, execute the ClientContext.ExecuteQuery() method.
    [Parameter(Mandatory = $false, Position = 4)]
    [switch]
    $executeQuery
)

begin { }
process {
    if ($PsCmdlet.ParameterSetName -eq "ClientObject") {
        $type = $object.GetType()
    } else {
        $type = $collectionObject.GetType() 
        if ($collectionObject -is [Microsoft.SharePoint.Client.ClientObjectCollection]) {
            $type = $collectionObject.GetType().BaseType.GenericTypeArguments[0]
        }
    }

    $exprType = [System.Linq.Expressions.Expression]
    $parameterExprType = [System.Linq.Expressions.ParameterExpression].MakeArrayType()
    $lambdaMethod = $exprType.GetMethods() | ? { $_.Name -eq "Lambda" -and $_.IsGenericMethod -and $_.GetParameters().Length -eq 2 -and $_.GetParameters()[1].ParameterType -eq $parameterExprType }
    $lambdaMethodGeneric = Invoke-Expression "`$lambdaMethod.MakeGenericMethod([System.Func``2[$($type.FullName),System.Object]])"
    $expressions = @()

    foreach ($propertyName in $propertyNames) {
        $param1 = [System.Linq.Expressions.Expression]::Parameter($type, "p")
        try {
            $name1 = [System.Linq.Expressions.Expression]::Property($param1, $propertyName)
        } catch {
            Write-Error "Instance property '$propertyName' is not defined for type $type"
            return
        }
        $body1 = [System.Linq.Expressions.Expression]::Convert($name1, [System.Object])
        $expression1 = $lambdaMethodGeneric.Invoke($null, [System.Object[]] @($body1, [System.Linq.Expressions.ParameterExpression[]] @($param1)))

        if ($collectionObject -ne $null) {
            $expression1 = [System.Linq.Expressions.Expression]::Quote($expression1)
        }
        $expressions += @($expression1)
    }


    if ($PsCmdlet.ParameterSetName -eq "ClientObject") {
        $object.Context.Load($object, $expressions)
        if ($executeQuery) { $object.Context.ExecuteQuery() }
    } else {
        $newArrayInitParam1 = Invoke-Expression "[System.Linq.Expressions.Expression``1[System.Func````2[$($type.FullName),System.Object]]]"
        $newArrayInit = [System.Linq.Expressions.Expression]::NewArrayInit($newArrayInitParam1, $expressions)

        $collectionParam = [System.Linq.Expressions.Expression]::Parameter($parentObject.GetType(), "cp")
        $collectionProperty = [System.Linq.Expressions.Expression]::Property($collectionParam, $parentPropertyName)

        $expressionArray = @($collectionProperty, $newArrayInit)
        $includeMethod = [Microsoft.SharePoint.Client.ClientObjectQueryableExtension].GetMethod("Include")
        $includeMethodGeneric = Invoke-Expression "`$includeMethod.MakeGenericMethod([$($type.FullName)])"

        $lambdaMethodGeneric2 = Invoke-Expression "`$lambdaMethod.MakeGenericMethod([System.Func``2[$($parentObject.GetType().FullName),System.Object]])"
        $callMethod = [System.Linq.Expressions.Expression]::Call($null, $includeMethodGeneric, $expressionArray)

        $expression2 = $lambdaMethodGeneric2.Invoke($null, @($callMethod, [System.Linq.Expressions.ParameterExpression[]] @($collectionParam)))

        $parentObject.Context.Load($parentObject, $expression2)
        if ($executeQuery) { $parentObject.Context.ExecuteQuery() }
    }
}
end { }
}

这篇关于如何在PowerShell中执行lambda表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆