在 Windows 和 Silverlight 类库之间共享 C# 代码 [英] Sharing C# code between Windows and Silverlight class libraries

查看:26
本文介绍了在 Windows 和 Silverlight 类库之间共享 C# 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们编写了一个小的 Windows 类库,它实现了一些标准类型(最初是字符串)的扩展方法.我把它放在一个库中,这样我们的任何项目都可以通过简单地引用它并使用 XXX.Extensions 添加来使用它.

We wrote a small Windows class library that implements extension methods for some standard types (strings initially). I placed this in a library so that any of our projects would be able to make use of it by simply referencing it and adding using XXX.Extensions.

当我们想在 Silverlight 中使用其中一些方法时出现了问题.尽管所有代码都是兼容的,但不能在 Silverlight 中引用 Windows 库,因此我们创建了一个 Silverlight 库,该库具有指向相同类文件的链接,并将编译器指令放入类中以允许不同的 using 声明和命名空间.直到今天,当我向 Windows 扩展库添加一个新类并意识到我还必须记住将类链接到 Silverlight 库时,这一切正常.

A problem came up when we wanted to use some of these methods in Silverlight. Although all the code was compatible, a Windows library can't be referenced in Silverlight so we created a Silverlight library that had links to the same class files and put compiler directives into the classes to allow different using declarations and namespaces. This worked fine until today when I added a new class to the Windows extensions library and realised that I would have to remember to link the class into the Silverlight library too.

这并不理想,我想知道是否有人对在 Windows 和 Silverlight 项目之间共享扩展方法和其他帮助程序代码的更好方法有想法.

This isn't ideal and I wondered if anyone might have ideas for a better way of sharing extension methods and other helper code between Windows and Silverlight projects.

推荐答案

您不能将 Silverlight 程序集的引用设置为常规 .NET 程序集,但您可以反过来这样做.

You cannot set a reference from a Silverlight assembly to a regular .NET assembly but you can do so the other way round.

因此,创建一个共享的 Silverlight 程序集并将您的代码添加到该程序集.现在,您可以将常规 .NET 和其他 Silverlight 程序集的引用设置为共享 Silverlight 程序集.

So create a shared Silverlight assembly and add your code to that assembly. Now you can set a reference fro both your regular .NET and you other Silverlight assembly to the shared Silverlight assembly.

限制是你只能将代码放在那里,同时适用于 .NET 和 Silverlight CLR,但这与共享代码没有什么不同.

The restriction is that you can only put code in there that would work on both the .NET and Silverlight CLR but that is no different from sharing code.

这篇关于在 Windows 和 Silverlight 类库之间共享 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆