代码之家  ›  专栏  ›  技术社区  ›  GatewayToCode

与两个dll相互通信,但彼此不知道

  •  -1
  • GatewayToCode  · 技术社区  · 7 年前

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

    我想处理两种不同的类型。这些类型生成为两个不同的dll。我通过XML序列化实例化了其中许多类型。第三方应用程序加载dll并开始从给定的xml文件创建实例。然后我遍历实例并从dll调用一个函数来执行类似导出的操作。在处理时,我得到了要共享到下一个实例的全局数据。问题是,他们不知道全球数据。他们只有一个函数参数类型(对象)。如果在每个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时都更新第三方应用程序。

    1 回复  |  直到 7 年前
        1
  •  0
  •   GatewayToCode    7 年前

    我想我得到了答案: 我将使结构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都会越来越大。 我的生成器给出的保证是,在每个dll中都是相同的结构类型。

    也许这篇文章更清楚地说明了这个问题。如果有一个更简单的方法或更少的过载,如反序列化,请让我知道。