简体   繁体   English

在多个服务和.net版本之间重用类型

[英]Reusing types across multiple services and .net versions

I have Services (Service1, Service2, ...) all which reference a Common.dll all of which are in .Net 4.0 , and I am having trouble reusing the types in the consumer of the service. 我有全部引用Common.dll的Services(Service1,Service2等),都在.Net 4.0中 ,并且在重用服务使用者中的类型时遇到了麻烦。


Common.dll has Common.dll有

Identifier {
    int Id;
    string Type;
}

Each Service implements 每个服务实现

byte[] Get(Common.Identifier);
string Test();

The consumer of the service ( .Net3.5 ) generates code in reference.cs 服务的使用者( .Net3.5 )在reference.cs中生成代码

class Service1 {
    byte[] Get(Service1.Identifier);
    string Test();  
}

class Service2 {
    byte[] Get(Service2.Identifier);
    string Test();  
}

I tied them together by creating an Interface Adding the interface to partial classes, but can only get it to work for the Test() call 我通过创建一个接口将它们绑在一起,将接口添加到部分类中,但是只能使它适用于Test()调用

public interface IService {
    //byte[] Get(Service2.Identifier);
    string Test();  
}

public partial class Service1 : IService;
public partial class Service2 : IService;

This way I can use the services interchangably. 这样,我可以互换使用服务。 I plan on creating many more to use as basically plugins to integrate to different systems 我计划创建更多用作基本插件以集成到不同系统的插件

IService GetService(string provider) {
    switch (provider) {
        case "Service1":
            return (IService)new Service1();

        case "Service2":
            return (IService)new Service2();
    }
}

GetService.Test() //works perfectly.

My problem is how do I define, decorate "Identifier" in such a way that will allow me to use the Get(?? Identifier) without writing a bunch of code. 我的问题是如何定义,装饰“标识符”,使我无需编写大量代码即可使用Get(??标识符)。

The only way I can think of right now is to create an interface IIdentifier, and then add to the partial classes 我现在唯一想到的方法是创建一个接口IIdentifier,然后将其添加到部分类中

public partial class Service1 : IService {
    byte[] Get(IIdentifier id) {
        return this.Get(new Service1.Identifier() { Id = id.Id, Type = id.Type});
}

but there are a lot of calls and I don't want to have to wrap them all. 但是有很多电话,我不想全部打包。 I'm sure I'm just missing something simple, thanks for any help that can be provided. 我确定我只是缺少一些简单的东西,感谢您可以提供的任何帮助。

I was approaching this from the wrong side, and the way I was trying to do it wouldn't work as Alexei pointed out. 我从错误的角度来解决这个问题,而我尝试这样做的方式却无法像Alexei所指出的那样起作用。

I managed to get everything to work and reuse the classes in the common.dll by compiling it for .net 3.5 instead of 4.0. 通过将其编译为.net 3.5(而不是4.0),我设法使所有功能正常工作并重用了common.dll中的类。

.Net 4.0 is backwards compatible with 3.5 so I was able to reference the 3.5 common.dll from my services which allowed my 3.5 code to reference common.dll and thus reuse the types. .Net 4.0向后与3.5兼容,因此我能够从服务中引用3.5 common.dll,这使我的3.5代码可以引用common.dll,从而重用类型。

C# does not provide a way to use different types as the same type even if members are identical in your particular case. 即使在特定情况下成员相同,C#也无法提供将不同类型用作同一类型的方法。 To fix you would need to either rewrite your services to use shared class or map classes into each other, possibly adding extension methods to make code look as if Get accepts the same type. 要解决此问题,您将需要重写服务以使用共享类或将映射类相互映射,可能需要添加扩展方法以使代码看起来像Get接受相同类型。

If you would not need to pass unique parameters but just call non-virtual/non-interface method you could have used dynamic ( dynamic service = ....; service.Get(); ) which is closest to duck typing as you can get here. 如果您不需要传递唯一的参数,而只是调用非虚拟/非接口方法,则可以使用dynamicdynamic service = ....; service.Get(); ),它尽可能地接近鸭子类型。过来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM