在c#中的对象之间传递数据

Rag*_*mar 5 c# data-binding reflection dll dynamic

我正在开发一个类似于基于插件的系统的桌面应用程序.我有一个客户端模块,它将加载包含'Machine'对象的DLL.此后,"机器"对象被操纵并按照良好定义的接口使用.

棘手的部分是包含"机器"对象的DLL是通过使用另一个程序动态生成的.在它的核心,DLL生成应用程序接受用户输入,生成类,在c#代码文件(其中包含用户指定的字段,我没有任何先验知识),并将这些类编译为DLL(机器. DLL文件).

客户端程序获取此dll,动态加载它然后在此机器对象上运行.

我在为解决在Machine对象和Client程序之间传递数据的问题建模解决方案时遇到了很多麻烦,主要是因为我不知道机器对象中包含哪些数据.

客户端程序要做以下事情.

- 加载dll并实例化'machine'对象. - 调用"机器"对象中的一系列函数,这些函数通过接口为客户端所知. - 从"机器"对象中提取各种变量并将其显示给用户.

我无法执行最后一步.

注意:我编写了一个简单的解决方案,其中有关字段的元数据由dll生成程序生成并存储在xml文件中.客户端程序使用这些xml文件来获取有关存储在计算机对象中的字段的信息.然后,它使用机器对象上的反射来访问对象的字段.

我觉得这很麻烦而且很慢.这种东西有任何模式或方法吗?

Mau*_*ves 3

当我读到这篇文章时想到的一个解决方案是利用 C# 中对属性的内置支持。属性是一种用一些附加元数据标记属性、字段、方法、类等的方法,然后由其他类使用,例如在序列化期间。你会在那里最常看到它。

我正在构建一个应用程序,该应用程序需要能够获取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;
    }
}
Run Code Online (Sandbox Code Playgroud)

通过像这样定义这个类,我可以像这样装饰我的数据类属性(实际属性已更改,以免迷失在业务术语中):

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; }
}
Run Code Online (Sandbox Code Playgroud)

因此,在我的导出例程中,我没有对可变的属性名称进行硬编码,也没有传递一个复杂的数据结构,其中包含有关要显示或隐藏哪些字段以及顺序的信息,我只是运行以下代码在本例中,使用反射循环属性并将其值输出到 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");
}
Run Code Online (Sandbox Code Playgroud)

OrderedProperties 结构的代码如下:

struct OrderedProperties
{
    /// <summary>
    /// 
    /// </summary>
    public int OrderByValue;
    /// <summary>
    /// 
    /// </summary>
    public string PropertyName;
    /// <summary>
    /// 
    /// </summary>
    public string Header;
    /// <summary>
    /// 
    /// </summary>
    public bool Export;
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,提取属性值的逻辑完全不考虑类的结构。它所做的就是找到用我创建的属性装饰的属性,并使用它来驱动处理。

我希望这一切都有意义,如果您需要更多帮助或澄清,请随时询问。