如何将参数传递给IEnumerable [英] How to pass parameters to IEnumerable

查看:94
本文介绍了如何将参数传递给IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类

[DataContract(Name = "SearchRequestArgs", Namespace = "")]
public class SearchRequestArgs
{
    [DataMember(Name = "UserType", Order = 1)]
    public Actor UserType { get; set; }

    [DataMember(Name = "SearchCriteria", Order = 2)]
    public IEnumerable<KeyValuePair<string, string>> SearchCriteria { get; set; }
}

所以我必须从单元测试类传递数据



所以对于SearchCriteria我传递静态数据?



请举个例子?



谢谢

So I have to pass data from unit test class

so for SearchCriteria I pass static dat?

Please give me an example?

Thank You

推荐答案

SearchCriteria 是一个属性..您可以读取/写入值...



超过 IEnumerable 只接受一个 out 类型参数

所以你可以修改为

SearchCriteria is a Property .. either you can Read/Write values to it...

more over IEnumerable accepts only one out Type Parameter
so you can modify as
public IEnumerable<string> SearchCriteria { get; set; }</string>





读/写你可以这样做





to read / Write you can do like this

var obj = new SearchRequestArgs();





价值



to write value

obj.SearchCriteria = some value ;// of type Ienumerable of type string ,like array ,list ....





value ...



to read value...

var someobj = obj.SearchCriteria ;


你向我们展示的是一个类定义,其中包含与序列化相关的属性,以及两个公共属性。



你可以创建此类的实例和您然后可以在创建的实例中设置公共属性的值。您创建的实例可以是静态的也可以是动态的。通常,传递参数一词是指在调用方法时提供给方法的参数



'SearchCriteria属性是一个IEnumerable KeyValue Pairs,其中Key和Value都是Type'字符串。您可以在Class的实例中设置该Property的值,其中任何Collection都是相同Type类型的IEnumerable,例如泛型列表,无论是静态还是非静态。



此代码的每一行都是legal:
What you show us is a Class definition with Attributes related to Serialization, and two Public Properties.

You can create instances of this Class, and you can then set the values of the Public Properties in the created instances. Those instances you create can be static or dynamic. Usually the words "pass parameters" refer to the arguments supplied to a method when it is invoked.

The 'SearchCriteria property is an IEnumerable of KeyValue Pairs, where both Key and Value are of Type 'string. You can set the value of that Property in an instance of the Class with any Collection that is an IEnumerable of the same Type structure, such as a generic list, whether it is static or non-static.

Every line of this code is "legal:"
// in Form scope
private SearchRequestArgs search1 = new SearchRequestArgs();

private static SearchRequestArgs search2 = new SearchRequestArgs();

private List<KeyValuePair<string, string>> listKVP1 = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("k1","v1"),
    new KeyValuePair<string, string>("k2","v2")
};

static List<KeyValuePair<string, string>> listKVP2 = new List<KeyValuePair<string, string>>
{
    new KeyValuePair<string, string>("k1","v1"),
    new KeyValuePair<string, string>("k2","v2")
};

// execute inside some method ...
search1.SearchCriteria = listKVP2; // assign static list to dynamic instance
search1.SearchCriteria = listKVP1; // assign dynamic list to dynamic instance
search2.SearchCriteria = listKVP1; // assign dynamic list to static instnace
search2.SearchCriteria = listKVP2; // assign static list to static instance

所以,我看不到静态如何成为问题。

So, I don't see how "static" is an issue here.


这篇关于如何将参数传递给IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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