2012-12-14 52 views
5

我正在开发一个类似于基于插件的系统的桌面应用程序。我有一个客户端模块,它会加载一个包含'Machine'对象的DLL。此后,按照定义良好的界面操作和使用'机器'对象。在c中的对象之间传递数据#

棘手的部分是,包含'机器'对象的DLL是通过使用其他程序即时生成的。在它的核心,DLL生成应用程序接受用户输入,在c#代码文件(包含用户指定的字段,我没有任何先前的知识)中生成类,并将这些类编译到DLL(机器)。 dll文件)。

客户端程序拿起这个DLL,动态加载它,然后在这台机器上运行。

我正面临着很多困难,建模解决方案来解决Machine对象和客户端程序之间传递数据的问题,实质上是因为我不知道机器对象中包含的数据。

客户端程序将执行以下操作。

- 加载dll并实例化'machine'对象。 - 通过接口调用客户端已知的“机器”对象中的一系列函数。 - 从“机器”对象中提取各种变量并将其显示给用户。

我无法执行最后一步。

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

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

+0

你看过托管扩展性框架(MEF)吗? http://mef.codeplex.com/ http://msdn.microsoft.com/en-us/library/dd460648.aspx – spender

+0

我听说过它,我对它的用法有个简单的概念。问题是,我对编程不熟悉,我想实现一个易于移植到其他语言和系统的解决方案。另外,我想真正学会实施这些复杂的系统。 –

+0

在C#中实现它使得它可以移植到其他系统,因为有Mono,它是在Linux和其他平台上运行的.Net框架的一个端口。我并不一定担心它会变得如此通用,尽管它可以快速转换到其他语言。每种语言和平台都有自己的一套成语和最佳实践,而在一种语言和平台中运行良好的东西在另一种语言和平台中可能效果不佳。 –

回答

3

说来,当我读心目中的解决方案这是为了利用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; 
} 

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

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

+0

您正在生成代码而不知道发送给您的对象中的属性是什么。所以要在我的程序中使用您的解决方案,我必须更改我的代码生成模块,以将这些属性包含在我即时生成的.cs文件中。我现在将执行此操作。谢谢。但是,你是否知道其他方法以更低级别的方式做同样的事情?编译器不是在做我正在做的事情,而是更具内在性?我怀疑它是否也在使用反射。我对吗?? –

+1

是的,那是对的。但是,权衡是一个更简单的处理集。将属性视为字段和方法的可堆栈元数据。只要你知道什么样的属性值得期待,你就可以完成数据的传递,并且在知道返回给你的实际对象的时候显示很少的数据。 [另外,如果需要,可以在运行时设置属性的值。](http://stackoverflow.com/questions/51269/change-attributes-parameter-at-runtime) –

+0

你知道如何实现上述解决方案在C/C++?或者也许是与你所做的相关的文档?我坚持使用c/C++,因为使用这些语言进行编码使得在更简单的层面上理解解决方案的工作变得非常容易。 –

2

您还可以选择ZeroMQ,这是一款轻量级MQ软件,支持应用程序之间的通信。 ZeroMQ只包含1个DLL,你可以嵌入你的任何应用程序中。

ZeroMQ拥有所有类型的客户端,包括C,C++,.NET,Python和Java的,红宝石,并且可以在这两个Windows/Linux的/ OSX运行..

+0

是不是ZeroMQ更适合网络应用?我的应用程序是桌面应用程序。 –

+3

不,它适用于应用程序,但不适用于Web,它是嵌入式的。 –

+0

好的。似乎使用套接字将与使用反射一样表现密集。 –