首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用对象初始化器初始化WebClient?

如何使用对象初始化器初始化WebClient?
EN

Stack Overflow用户
提问于 2015-12-01 09:02:46
回答 2查看 2K关注 0票数 4

我有一个这样的WebClient

代码语言:javascript
复制
WebClient _webClient = new WebClient
{
    UseDefaultCredentials = true,
    Encoding = System.Text.Encoding.UTF8,               
};
_webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

我希望使用对象初始化器初始化Headers

代码语言:javascript
复制
WebClient _webClient = new WebClient
{
    UseDefaultCredentials = true,
    Encoding = System.Text.Encoding.UTF8,
    Headers = new WebHeaderCollection
    {
        "user-agent",
        "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)",
    }
};

但我不知道怎么写。有可能吗?

2016/07/07/07更新

是关于索引器对象和集合初始化器的。在普通集合(例如List<int> numbers )中,您可以通过以下代码插入它

代码语言:javascript
复制
    List<int> numers = new List<int> {
        1, 2, 3
    };

但是,我们想要插入的名称头的WebHeaderCollectio不是List<...>,而是WebClient中的一个泛型属性

代码语言:javascript
复制
public class WebClient : Component
{
    ...
    public WebHeaderCollection Headers { get; set; }
    ...
}

对象和集合初始化器提到集合初始化器允许您在初始化实现IEnumerable的集合类或具有添加扩展方法的类时指定一个或多个元素初始化器。

所以让我们检查一下WebHeaderCollection

代码语言:javascript
复制
public class WebHeaderCollection : NameValueCollection, ISerializable
{
    public string this[HttpResponseHeader header] { get; set; }

    public string this[HttpRequestHeader header] { get; set; }

    public void Add(string header);

    public void Add(HttpRequestHeader header, string value);

    public void Add(HttpResponseHeader header, string value);
}

WebHeaderCollection : NameValueCollection : NameObjectCollectionBase : IEnumerable

最后是我的练习样本

代码语言:javascript
复制
 class Program
    {
        static void Main(string[] args)
        {
            PeopleEnumerable peopleEnumerable = new PeopleEnumerable {
                { "Michael", "1" },
                { "Mike", "2" },
                { "Butters;3" },
            };

            Console.WriteLine(peopleEnumerable["1"]);
            Console.WriteLine(peopleEnumerable["2"]);
            Console.WriteLine(peopleEnumerable["3"]);
        }
    }

    public class PeopleEnumerable : IEnumerable
    {
        Dictionary<string, string> _peopels = new Dictionary<string, string>();

        public string this[string id]
        {
            get
            {
                return _peopels[id];
            }
            set
            {
                _peopels[id] = value;
            }
        }

        public void Add(string name, string id)
        {
            _peopels.Add(id, name);
        }
        public void Add(string nameAndId)
        {
            var name = nameAndId.Split(';')[0];
            var id = nameAndId.Split(';')[1];

            _peopels.Add(id, name);
        }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }

您需要实现'System.Collections.IEnumerable',否则无法使用集合初始化器初始化'PeopleEnumerable‘类型,因为它没有实现PeopleEnumerable。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-01 09:15:02

你可以这样做。必须用冒号(:)拆分属性及其值:

代码语言:javascript
复制
WebClient _webClient = new WebClient
    {
        UseDefaultCredentials = true,
        Encoding = System.Text.Encoding.UTF8,
        Headers = new WebHeaderCollection
        {
            "user-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"
        }
    };
票数 4
EN

Stack Overflow用户

发布于 2016-06-24 05:51:16

我认为您也可以这样做(没有头中的: ):

代码语言:javascript
复制
private static WebClient _doClient = new WebClient
{
    BaseAddress = "https://api.digitalocean.com/v2/domains/example.com/",
    Headers = new WebHeaderCollection { { "Authorization", "Bearer " + _digitalOceanApiKey } }
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34016386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档