1

We publish a class library that must remain compatible with .NET 2.0. However, we would also like to use this class library internally for WCF-based projects.

Reading e.g. expose-object-from-class-library-using-wcf brings up an approach for using 2.0 class libraries by creating DataContractSurrogate objects to map 2.0 classes. However, our object hierarchy is rich and deep, and this would be tedious and difficult to maintain.

Since we do control the source code, I'm looking for an easier approach.

One thought is to add the DataConract attribute to the source code, and provide an "empty" implementation of DataContractAttribute when the solution is built targeting the 2.0 runtime, and using the System.Runtime.Serialization implementation when the solution is built targeting 3.0 and newer runtimes. This could be accomplished by manually tweaking references between builds.

Is there a better way to implement this approach?

Is there a better approach?

4

2 回答 2

1

另一种方法是添加 WCF 属性(DataContractAttribute 等),并使用 .NET 3.0 程序集进行构建。

当您在 .NET 2.0 下运行应用程序时,具有在运行时不可用的程序集的属性通常不会导致问题。

(如果您实际尝试访问这些属性,您会得到一个错误,但这不太可能而且很容易避免)。

于 2010-01-11T19:20:36.273 回答
0

您可以通过代码生成或 Visual Studio 中提供的 T4 模板来实现您的目标。另一方面,我不确定是否可以使用 T4 调整 Visual Studio 项目文件。另一个可能的缺点是 T4 脚本的调试比普通的 cs 文件更繁琐。

于 2010-01-11T19:22:23.607 回答