说来,当我读心目中的解决方案这是为了利用C#中的Attributes的内置支持。属性是一种标记属性,字段,方法,类等的方法,其中包含一些其他元数据,然后由其他类使用,例如在Serialization期间。你会经常在那里看到它。
我有一个应用程序,我正在建设,需要能够采取IEnumerable
对象集合,并输出一些数据到文件基于用户选择的选择。我创建了一个属性类,使我能够通过反射来阅读选择,并按照指示行事。让我告诉你的例子:
首先是属性类:
[System.AttributeUsage(AttributeTargets.Property)]
class ExportOptionsAttribute : System.Attribute
{
public string Header { get; set; }
public string FormatString { get; set; }
public bool Export { get; set; }
public int Order { get; set; }
/// <summary>
///
/// </summary>
/// <param name="header"></param>
public ExportOptionsAttribute(string header) : this (header, null, true)
{
}
/// <summary>
///
/// </summary>
/// <param name="header"></param>
/// <param name="formatString"></param>
/// <param name="export"></param>
public ExportOptionsAttribute(string header, string formatString, bool export)
{
this.Header = header;
this.FormatString = formatString;
this.Export = export;
this.Order = 0;
}
}
,象这样定义这个类,我可以装饰我这样的数据类属性(实际性改变,以便不会迷路上商业行话):
public sealed class PartsOrder
{
/// <summary>
///
/// </summary>
[ExportOptions("Customer Name", Order=0)]
public string CustomerName { get; set; }
/// <summary>
///
/// </summary>
[ExportOptions("Catalog Name", Order = 1)]
public string Catalog Name { get; set; }
/// <summary>
///
/// </summary>
[ExportOptions("Unit", Order = 2)]
public string Unit { get; set; }
/// <summary>
///
/// </summary>
[ExportOptions("Component", Order = 3)]
public string Component { get; set; }
/// <summary>
///
/// </summary>
[ExportOptions("Delivery Point", Order = 4)]
public string DeliveryPoint { get; set; }
/// <summary>
///
/// </summary>
[ExportOptions("Order Date", Order = 5)]
public string OrderDate { get; set; }
}
所以后来在我出口常规,而不是硬编码的属性名称,它是可变的,或经过一个复杂的数据结构,在其周围包含了哪些字段,以显示或隐藏信息以及排序是什么,我在这种情况下,只需运行以下代码(使用反射)将属性循环并将其值输出到CSV文件。
StringBuilder outputDoc = new StringBuilder();
// loop through the headers in the attributes
// a struct which decomposes the information gleaned from the attributes
List<OrderedProperties> orderedProperties = new List<OrderedProperties>();
// get the properties for my object
PropertyInfo[] props =
(typeof(PartsOrder)).GetProperties();
// loop the properties
foreach (PropertyInfo prop in props)
{
// check for a custom attribute
if (prop.GetCustomAttributesData().Count() > 0)
{
foreach (object o in prop.GetCustomAttributes(false))
{
ExportOptionsAttribute exoa = o as ExportOptionsAttribute;
if (exoa != null)
{
orderedProperties.Add(new OrderedProperties() { OrderByValue = exoa.Order, PropertyName = prop.Name, Header = exoa.Header, Export = exoa.Export });
}
}
}
}
orderedProperties = orderedProperties.Where(op => op.Export == true).OrderBy(op => op.OrderByValue).ThenBy(op => op.PropertyName).ToList();
foreach (var a in orderedProperties)
{
outputDoc.AppendFormat("{0},", a.Header);
}
// remove the trailing commma and append a new line
outputDoc.Remove(outputDoc.Length - 1, 1);
outputDoc.AppendFormat("\n");
var PartsOrderType = typeof(PartsOrder);
//TODO: loop rows
foreach (PartsOrder price in this.Orders)
{
foreach (OrderedProperties op in orderedProperties)
{
// invokes the property on the object without knowing the name of the property
outputDoc.AppendFormat("{0},", PartsOrderType.InvokeMember(op.PropertyName, BindingFlags.GetProperty, null, price, null));
}
// remove the trailing comma and append a new line
outputDoc.Remove(outputDoc.Length - 1, 1);
outputDoc.AppendFormat("\n");
}
为OrderedProperties结构的代码是在这里:
struct OrderedProperties
{
/// <summary>
///
/// </summary>
public int OrderByValue;
/// <summary>
///
/// </summary>
public string PropertyName;
/// <summary>
///
/// </summary>
public string Header;
/// <summary>
///
/// </summary>
public bool Export;
}
正如你可以看到,提取属性值的逻辑是完全不知道的类的结构。它所做的就是查找用我创建的属性装饰的属性,并使用它来驱动处理。
我希望这一切都有道理,如果您需要更多帮助或澄清,请随时询问。
你看过托管扩展性框架(MEF)吗? http://mef.codeplex.com/ http://msdn.microsoft.com/en-us/library/dd460648.aspx – spender
我听说过它,我对它的用法有个简单的概念。问题是,我对编程不熟悉,我想实现一个易于移植到其他语言和系统的解决方案。另外,我想真正学会实施这些复杂的系统。 –
在C#中实现它使得它可以移植到其他系统,因为有Mono,它是在Linux和其他平台上运行的.Net框架的一个端口。我并不一定担心它会变得如此通用,尽管它可以快速转换到其他语言。每种语言和平台都有自己的一套成语和最佳实践,而在一种语言和平台中运行良好的东西在另一种语言和平台中可能效果不佳。 –