内联语句键对值 [英] Inline statement keypairvalues

查看:78
本文介绍了内联语句键对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HtmlLink:

I have the following HtmlLink:

HtmlLink htmlLink = new HtmlLink(); 
htmlLink.Attributes.Add("href", "/style/" + category + ".css"); 
htmlLink.Attributes.Add("type", "text/css"); 
htmlLink.Attributes.Add("rel", "stylesheet"); 

我正在尝试在一行语句中编写它,但是我不知道编写如下语句的正确语法:

Im trying to write it in a one line statement, but I dont know the right syntax to write a statement like:

List<string> list = new List<string> { "one", "two", "three" };

正在做

HtmlLink htmlLink = new HtmlLink() { ???? };

有人可以满足我的好奇心吗?

Can someone satisfy my curiosity?

推荐答案

据我所知无法完成.您总是可以编写扩展方法:

As far as I can tell it can't be done. You could always write an extension method:

public static void AddAttributes(this HtmlLink thisLink, IDictionary<string, string> attributes)
{
  foreach(var attribute in attributes.Keys)
  {
    thisLink.Attributes.Add(attribute.Key, attribute.Value);
  }
}

用法:

var link = new HtmlLink();
link.AddAttributes(new Dictionary<string,string>{{"Key1","Value1"},{"Key2","Value2"}};

虽然与多个添加相比,不确定其具有很大的优势.另一个选择是在某个地方编写一个辅助方法,该方法可以接受字典并吐出带有属性集的HtmlLink.

Not sure this has a great advantage over multiple adds though. Another option is to write a helper method somewhere that accepts a dictionary and spits out an HtmlLink with its attributes set.

这篇关于内联语句键对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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