可能重复:
获取Enum值的属性
我有一个带有Description属性的枚举,如下所示:
public enum MyEnum
{
Name1 = 1,
[Description("Here is another")]
HereIsAnother = 2,
[Description("Last one")]
LastOne = 3
}
Run Code Online (Sandbox Code Playgroud)
我找到了一些用于根据Enum检索描述的代码
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];
if (attributes != null && attributes.Any())
{
return attributes.First().Description;
}
return value.ToString();
}
Run Code Online (Sandbox Code Playgroud)
这允许我编写如下代码:
var myEnumDescriptions = from MyEnum n in Enum.GetValues(typeof(MyEnum))
select new { ID = (int)n, Name = Enumerations.GetEnumDescription(n) };
Run Code Online (Sandbox Code Playgroud)
我想要做的是,如果我知道枚举值(例如1) - 我该如何检索描述?换句话说,如何将整数转换为"枚举值"以传递给我的GetDescription方法?
我一直在尝试为我的网站收集分析,并意识到谷歌分析并未设置为访问www.example.com的访问者捕获数据(它仅为example.com设置).我注意到,当我只键入example.com时,许多网站会将我重定向到www.example.com.但是,stackoverflow正好相反(将www.stackoverflow.com重定向到stackoverflow.com).
所以,我已经决定,为了获得准确的分析,我应该让我的Web服务器将所有用户重定向到www.example.com或example.com.是否有理由做其中一个?这纯粹是个人偏好吗?与www有什么关系?我在浏览器中键入域时从不输入.
鉴于我有生日/周年纪念日期时间,如何确定该日期是否在特定日期范围内发生?例如,
生日= 1/2/2000
日期范围= 12/25/2008 - 1/3/2009
我需要一种方法来确定这个人的生日是否发生在该日期范围内 - 最好是在C#中.
我首先要改变生日DateTime的年份以匹配日期范围,然后检查"新"生日DateTime是否在日期范围的开始日期和结束日期之间...但是当日期范围跨越不同年份时,就像我上面的例子 - 我不得不添加一个讨厌的if语句.有没有更好的方法?
我创建了一个全新的Rails 3.1应用程序.我在app/assets/stylesheets/bootstrap.min.css中添加了twitter bootstrap CSS文件.这是相关的代码
app/assets/stylesheets/application.css(包括树,因此包含bootstrap)
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require_tree .
*/
Run Code Online (Sandbox Code Playgroud)
Gemfile(包括用于编译/压缩的execjs和therubyracer)
group :development, :qa do
gem 'execjs'
gem 'therubyracer'
end
# Gems used only for assets …Run Code Online (Sandbox Code Playgroud) 我正在将一串 XML 解析成一个看起来像这样的 XDocument(使用 XDocument.Parse)
<Root>
<Item>Here is "Some text"</Item>
</Root>
Run Code Online (Sandbox Code Playgroud)
然后我对 XML 进行了一些操作,我想将它作为字符串发送回来,就像它进来一样
<Root>
<Item>Here is "Some text"</Item>
<NewItem>Another item</NewItem>
</Root>
Run Code Online (Sandbox Code Playgroud)
然而,我得到的是
<Root>
<Item>Here is \"Some text\"</Item>
<NewItem>Another item</NewItem>
</Root>
Run Code Online (Sandbox Code Playgroud)
注意双引号现在是如何转义而不是编码的?
无论我使用
ToString(SaveOptions.DisableFormatting);
Run Code Online (Sandbox Code Playgroud)
或者
var stringWriter = new System.IO.StringWriter();
xDoc.Save(stringWriter, SaveOptions.DisableFormatting);
var newXml = stringWriter.GetStringBuilder().ToString();
Run Code Online (Sandbox Code Playgroud)
我怎样才能让双引号出现"和不出现\"?
更新:也许这可以更好地解释它:
var origXml = "<Root><Item>Here is \"Some text"</Item></Root>";
Console.WriteLine(origXml);
var xmlDoc = System.Xml.Linq.XDocument.Parse(origXml);
var modifiedXml = xmlDoc.ToString(System.Xml.Linq.SaveOptions.DisableFormatting);
Console.WriteLine(modifiedXml);
Run Code Online (Sandbox Code Playgroud)
我从中得到的输出是:
<Root><Item>Here is "Some text"</Item></Root>
<Root><Item>Here is "Some text"</Item></Root>
Run Code Online (Sandbox Code Playgroud)
我希望输出是: …