本文介绍了我无法修改的库中的类的WCF DataContract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类库,该类库执行方法并具有许多不同的类,将其用作方法调用的参数...我正在为此类库创建一个wcf包装器.但我无权更改类库.

hi i have a class library that which performs methods and has a lot of different classes which it uses as parameters for the methods calls... i am creating a wcf wrapper for this class library. but I do not have permission to change the class library.

现在我的问题是我如何轻松地将这些类公开为数据协定/数据成员..?

now my question is how can i expose these classes as data contracts/datamembers easily .. ?

这些方法需要大约100个不同的类.

I have about 100 different classes which i need for those methods.

谢谢

推荐答案

如果您确实无法更改库,那么我相信通过WCF公开类的唯一选择是为每个方法创建包装对象.我会考虑为此任务编写代码生成器.

If you really can't change the library, then I believe your only choice to expose the classes via WCF is to create wrapper objects for each method. I would look at writing a code generator for this task.

您可以反映出要公开的程序集中的一组类型,以获取所需的类型元数据信息.

You can reflect over the set of types in the assembly that you want to expose to get the type metadata information that you need.

您可以使用t4模板(VS 2008及更高版本的一部分)和 T4工具箱之类的东西来创建代码生成器为您编写代码.生成器完成后,如果您的库发生了更改,应该很容易再次重新运行.修复更新代码生成器并重新运行它的错误也很容易.

You can use something like t4 templates (part of VS 2008 and above) and the T4 Toolbox to create the code generator to write the code for you. Once the generator is done it should be easy to rerun again if your library ever changes. It's also easy to fix bugs updating the code generator and rerunning it.

我仅出于完整性考虑而提到的另一个选项却存在一些棘手的问题,那就是反汇编并修补有问题的代码.您可以使用ildasm之类的东西来转储程序集的il,添加必要的WCF属性,然后使用ilasm重新组装它.但是,该过程可能容易出错,每当程序集更改时,您都必须重做该过程,则可能会出现法律问题,具体取决于谁拥有该程序集的IP,并且您必须重新签名该程序集,如果需要一个强名称的程序集,则可能具有不同的加密证书.

The other option which I mention only for completeness but which has some thorny issues would be to disassemble and patch the code in question. You can use something like ildasm to dump the il of the assembly, add the necessary WCF attribution and then reassemble it with ilasm. However, the process can be error prone, any time the assembly changes you'll have to redo the process, there could be legal issues depending on who owns the IP of the assembly, and you'll have to re-sign the assembly, potentially with a different cryptographic cert if it needs to be a strong-named assembly.

*编辑*

请求的包装器代码示例:

Requested wrapper code sample:

public class ToWrap {
  public String Name { get; set; }
  public String Address { get; set; }
}

[DataContract]
public class Wrapper {
  private ToWrap _wrapped;

  // constructor for WCF marshalling
  public Wrapper() {
    _wrapped = new ToWrap();
  }

  public Wrapper(ToWrap wrapped) {
    _wrapped = wrapped;
  }

  [DataMember]
  public String Name {
    get { return _wrapped.Name; }
    set { _wrapped.Name = value; }
  }

  [DataMember]
  public String Address {
    get { return _wrapped.Address; }
    set { _wrapped.Address = value; }
  }
}

这篇关于我无法修改的库中的类的WCF DataContract的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 19:40