与两个dll的相互通信,但彼此不认识



我需要一个在给定的dll中未知的结构作为函数参数。

我想处理两种不同的类型。类型被生成为两个不同的dll。我通过Xml序列化来实例化其中的许多类型。第三方应用程序加载dll并开始从给定的xml文件中创建实例。然后,我对实例进行迭代,并从dll中调用一个函数来执行类似导出的操作。在处理过程中,我得到了要共享到下一个实例的全局数据。问题是,他们不知道全球数据。他们只得到了一个函数参数typeof(object)。如果我在每个dll中实现相同的结构,我就无法将其强制转换到结构中,因为dll A和dll B不同。那我该怎么办?

//Third party application
object globalData = null; //Type is not known in this application
//serialisation here...
I_SVExternalFruitExport[] instances = serialisation(....);
foreach(I_SVExternalFruitExport sv in instances)
{
globalData = sv.ProcessMyType(globalData, sv);
}
//--------------------------------------------------------
// one DLL AppleExport implements I_SVExternalFruitExport
using Apple.dll
struct s_mytype // s_mytype  is known in this dll
{
List<string> lines;
...
}
local_sv;
public object ProcessMyType(object s_TypeStruct, object sv)
{
local_sv = (Apple)sv;
if(globalData != null) globalData = new s_mytype();
else globalData = (s_mytype)s_TypeData;
//Do Stuff
return globalData;
}

//--------------------------------------------------------
// second DLL OrangeExport  implements I_SVExternalFruitExport 
using Orange.dll
struct s_mytype    //s_mytype  is known in this dll
{
List<string> lines;
...
}
Orange local_sv;   // Orange is known because of using Orange.dll
public object ProcessMyType(object s_TypeStruct, object sv)
{
local_sv = (Orange)sv;
if(globalData != null) globalData = new s_mytype();
else globalData = (s_mytype)s_TypeData; //This cast says... s_TypeData is not s_mytype because of two dlls A and B but i know they have the same structure.
//Do Stuff
return globalData;
}

我需要一个在我的dll中已知但在第三方应用程序中未知的结构,因为我想重新生成我的dll,也许在结构中有更多信息。我不想每次更改dll时都更新我的第三方应用程序。

我想我得到了一个答案:我将使我的结构s_mytype{}也可序列化:

[Serializable]
public struct s_mytype
{
public List<string> lines;
[System.Xml.Serialization.XmlElementAttribute("Lines", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string[] Lines 
{ 
get { return lines.ToArray(); } 
set { lines.AddRange(value); } 
}
}

我的函数"ProcessMyType()"现在必须返回一个包含xml数据的字符串:

public string ProcessMyType(object s_TypeStruct, object sv)

现在唯一的问题是,"Apple"或"Orange"的每个实例都必须首先取消xml的私有化,并且随着每个实例的增加,xml变得越来越大。我的生成器给garantee,在每个dll中都是相同的structs_mytype。

也许这篇文章让问题变得更清楚了。如果有一种更简单的方法或像反序列化这样负载较少的方法,请告诉我。

最新更新