System.Linq.Enumerable.OfType< T>-有F#方式吗? [英] System.Linq.Enumerable.OfType<T> - is there a F# way?

查看:47
本文介绍了System.Linq.Enumerable.OfType< T>-有F#方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用F#WSDL类型提供程序.要调用我正在使用的Web服务,我需要将客户端凭据附加到 System.ServiceModel.Description.ClientCredentials .

这是我拥有的C#代码:

  var serviceClient = new InvestmentServiceV1Client.InvestmentServiceV1Client();foreach(serviceClient.Endpoint.Behaviors.OfType< ClientCredentials>()中的ClientCredentials行为){(行为).UserName.UserName = USERNAME;(行为).UserName.Password = PASSWORD;休息;} 

这是我到目前为止拥有的F#代码:

 让客户端=新服务.ServiceTypes.InvestmentServiceV1Client()让xxx = client.Endpoint.Behaviors|>顺序选择(fun p->火柴盒p与:?System.ServiceModel.Description.ClientCredentials为x->一些(x)_->没有任何)|>(System.ServiceModel.Description.ClientCredentials)p.UserName.UserName = USERNAME 

是否有F#等同于 System.Linq.Enumerable.OfType< T> 还是我应该只使用原始的 OfType< T> 吗?

我想问题主要是关于 break 构造的,在F#中不可用.好的,代码实际上只是为集合的第一个元素设置了用户名和密码(如果集合为空,则不设置用户名和密码).如果将集合转到F#列表,则可以使用模式匹配轻松完成此操作:

 //获取与C#中相同的行为,并使用"List.ofSeq"将其转换为列表让sc = new InvestmentServiceV1Client.InvestmentServiceV1Client()let behaviors = sc.Endpoint.Behaviors.OfType< ClientCredentials>()|>序列表//现在,我们可以使用模式匹配来查看列表中是否有内容将行为与|行为:: _->//如果列表为非空,请设置用户名和密码行为.用户名.用户名<-用户名行为.用户名.密码<-密码|_->() 

I'm looking to use the F# WSDL Type Provider. To call the web service I am using, I need to attach my client credentials to the System.ServiceModel.Description.ClientCredentials.

This is the C# code I have:

var serviceClient = new InvestmentServiceV1Client.InvestmentServiceV1Client();

foreach (ClientCredentials behaviour in serviceClient.Endpoint.Behaviors.OfType<ClientCredentials>())
{
    (behaviour).UserName.UserName = USERNAME;
    (behaviour).UserName.Password = PASSWORD;
    break;
}

This is the F# code I have so far:

let client = new service.ServiceTypes.InvestmentServiceV1Client()
let xxx = client.Endpoint.Behaviors
|> Seq.choose (fun p -> 
    match box p with   
    :?   System.ServiceModel.Description.ClientCredentials as x -> Some(x) 
    _ -> None) 
|> (System.ServiceModel.Description.ClientCredentials)p.UserName.UserName = USERNAME

Is there an F# equivalent of System.Linq.Enumerable.OfType<T> or should I just use raw OfType<T> ?

解决方案

I suppose the question is mainly about the break construct, which is not available in F#. Well, the code really just sets the user name and password for the first element of the collection (or none, if the collection is empty). This can be done easily using pattern matching, if you turn the collection to an F# list:

// Get behaviours as in C# and convert them to list using 'List.ofSeq'
let sc = new InvestmentServiceV1Client.InvestmentServiceV1Client()
let behaviours = sc.Endpoint.Behaviors.OfType<ClientCredentials>() |> List.ofSeq

// Now we can use pattern matching to see if there is something in the list
match behaviours with
| behaviour::_ ->
    // And if the list is non-empty, set the user name and password
    behaviour.UserName.UserName <- USERNAME
    behaviour.UserName.Password <- PASSWORD
| _ -> ()

这篇关于System.Linq.Enumerable.OfType&lt; T&gt;-有F#方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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