作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我拥有的一个类(class)的简化 View :
public class SalesMixRow
{
[DisplayFormat(DataFormatString = "{0:c0}")]
public decimal? DelSales { get; set; }
}
@Html.DisplayFor(model => model.DelSales)
void Main()
{
var model = new SmrDistrictModel
{
Title = "DFW",
SalesMixRow = new SalesMixRow
{
DelSales = 500m
}
};
Console.WriteLine(FollowPropertyPath(model, "Title"));
Console.WriteLine(FollowPropertyPath(model, "SalesMixRow.DelSales"));
}
public static object FollowPropertyPath(object value, string path)
{
Type currentType = value.GetType();
DisplayFormatAttribute currentDisplayFormatAttribute;
string currentDataFormatString = "{0}";
foreach (string propertyName in path.Split('.'))
{
PropertyInfo property = currentType.GetProperty(propertyName);
currentDisplayFormatAttribute = (DisplayFormatAttribute)property.GetCustomAttributes(typeof(DisplayFormatAttribute), true).FirstOrDefault();
if (currentDisplayFormatAttribute != null)
{
currentDataFormatString = currentDisplayFormatAttribute.DataFormatString;
}
value = property.GetValue(value, null);
currentType = property.PropertyType;
}
return string.Format(currentDataFormatString, value);
}
public class SmrDistrictModel
{
public string Title { get; set; }
public SalesMixRow SalesMixRow { get; set; }
}
public class SalesMixRow
{
[DisplayFormat(DataFormatString = "{0:c0}")]
public decimal? DelSales { get; set; }
}
最佳答案
您可以使用反射从类中检索属性。然后从属性中获取格式字符串,并使用 string.Format
应用它.
SalesMixRow instance = new SalesMixRow { DelSales=1.23 };
PropertyInfo prop = typeof(SalesMixRow).GetProperty("DelSales");
var att = (DisplayFormatAttribute)prop.GetCustomAttributes(typeof(DisplayFormatAttribute), true).FirstOrDefault();
if (att != null)
{
Console.WriteLine(att.DataFormatString, instance.DelSales);
}
System.ComponentModel.DataAnnotations.dll
,其中包含
DisplayFormat
属性。)
关于asp.net - 如何使用在控制台应用程序中应用的 DisplayFormat DataFormatString 输出属性(理想情况下按字符串查询对象)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13443643/
从许多帖子中我可以读到有关最大池大小的公式,它取决于 CPU 的数量 + 1。这是一个明确的解释。 但是如果应用程序定义了许多执行器或线程池执行器怎么办。然后我们还必须考虑应用程序共享相同的硬件。这对
我一直在寻找垂直 Accordion 内容 slider 。我找到了一个:http://www.marghoobsuleman.com/jQuery-common-accordion ,而且还不错。我
在 not having a whole bunch of luck 之后找到任何可充当 SNMP 代理的 .NET SNMP 库(可在 OSS 项目中使用 - 这意味着专有库已经过时),我现在正在考
目前我正在考虑: VMWare 管理程序 ESXi 中央操作系统 5.5 内存缓存 1.4.5和依赖 这就是全部。我还需要什么/任何替代品吗? 最佳答案 Memcached在linux上运行的很好,所
我是一名优秀的程序员,十分优秀!