支持 .Net 2.0 库中的 DataContracts
是否有可能创建一个可以从 .Net 2.0 使用的库,并且还允许 .Net 3.0+ 消费者从 DataContracts 中受益(如果他们愿意)?
除了包含不同 DataContract 相关属性的库是 .Net 3.0 库之外,我没有看到任何技术障碍。 这些属性可以像 ExtensionMethodAttribute 一样手动实现吗?
Is it possible to create a library that can be consumed from .Net 2.0 and also allows .Net 3.0+ consumers to benefit from DataContracts if they wish?
I don't see any technical obstacle for this other than the library that contains the different DataContract related attributes is a .Net 3.0 library. Can these attributes be implemented manually in similar way as ExtensionMethodAttribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您将 .NET 3.0 属性添加到程序集,然后在仅具有 .NET 2.0 的计算机上运行它,这相当于在您的代码上拥有来自程序集的属性,而该属性在运行时不可用。
只要 .NET 2.0 应用程序实际上不尝试读取属性,这基本上就可以正常工作。 例如,以下代码将抛出 FileNotFoundException,因为如果在使用 .NET 2.0 的计算机上运行,它将无法加载 System.ServiceModel.dll:
因此,如果您不知道谁将使用您的类库,则无法100% 保证您不会损坏它们。
通过将 System.ServiceModel.dll(或包含您正在使用的属性的其他程序集)的副本与您的应用程序一起发送,或者(我认为)通过使用采用 System.Type 参数的 GetCustomAttributes 重载,可以避免上述情况,因此您只读取已知可用的属性。
If you add .NET 3.0 attributes to an assembly, then run this on a machine with .NET 2.0 only, this amounts to having an attribute on your code from an assembly that is unavailable at runtime.
This will mostly work fine as long as the .NET 2.0 application doesn't actually attempt to read the attributes. For example, the following code will throw a FileNotFoundException because it will fail to load System.ServiceModel.dll if run on a machine with .NET 2.0:
Therefore if you don't know who will be consuming your class library, you can't guarantee 100% that you won't break them.
The above can be avoided by shipping a copy of System.ServiceModel.dll (or other assembly containing attributes you're using) with your application, or (I think) by using the overload of GetCustomAttributes that takes a System.Type argument, so you only read attributes that are known to be available.
由于 .NET 2.0 和 .NET 3.0 及 3.5 使用相同的 CLR (2.0),因此您应该不会遇到任何困难。
Since .NET 2.0 and .NET 3.0 and 3.5 use the same CLR (2.0), you shouldn't have any difficulties.