c# - 如何使用对象初始化器初始化 WebClient?

标签 c# asp.net

我有一个 WebClient像这样:

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使用对象初始值设定项:

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

关于indexerObject and Collection Initializers .在正常集合中,例如 List<int> numbers 你可以通过以下代码初始化

    List<int> numers = new List<int> {
        1, 2, 3
    };

但是WebHeaderCollectio我们要初始化的名称 header 不是 List<...>只是 WebClient 中的一个通用属性

public class WebClient : Component
{
    ...
    public WebHeaderCollection Headers { get; set; }
    ...
}

Object and Collection Initializers提到集合初始化器允许您在初始化实现 IEnumerable 的集合类或具有 Add 扩展方法的类时指定一个或多个元素初始化器。

所以让我们检查一下 WebHeaderCollection

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

最后是我的练习样本

 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();
        }
    }

您需要实现 IEnumerable,否则您将得到无法使用集合初始化程序初始化类型“PeopleEnumerable”,因为它没有实现“System.Collections.IEnumerable”

最佳答案

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

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;)"
        }
    };

关于c# - 如何使用对象初始化器初始化 WebClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34016386/

相关文章:

c# - 在编译时不知道类型的情况下将 IEnumerable<A> 转换为 IEnumerable<B>

c# - WPF 如何使列表框项目可编辑?

asp.net - 在 ASP.NET WebForms 中找出控件处于生命周期的哪个阶段

asp.net - 将 Gridview 导出到 Excel,并将行格式化为文本

asp.net - Gridview RowUpdating 事件未触发

c# - 具有月和年刻度的时间间隔的 TimeSpan 对象的绝对差

c# - LINQ 中的 DataContext 提交更改

c# - 正则表达式将数字提取到组中

c# - .mdf"failed with the operating system error 2(系统找不到指定的文件。)

.net - 我能否使用 ASP.NET MVC 获得与 ASP.NET (Webforms) 完全相同的用户体验?