在Silverlight服务器和客户端项目之间共享类 [英] Sharing classes between Server and Client projects in Silverlight

查看:123
本文介绍了在Silverlight服务器和客户端项目之间共享类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:B级A级RIA服务的一个子类对象返回BS的列表。 A类和B都必然在服务器侧限定。他们序列罚款,我可以在主客户端项目中使用它们。

Problem: Class B is a subclass of Class A. RIA service returns a list of object Bs. Class A and B are both necessarily defined on the server-side. They serialize fine, and I can use them in the primary client project.

我有其他的两个库,组织为客户端库。一个是自定义控件,另一个是对于那些自定义控件和实际的客户端项目之间共享类。

I have two other libraries, organized as client libraries. One is for custom controls, and the other is for classes that are shared between custom controls and the actual client project.

我需要一个类是从类库访问客户方(以便自定义控件可以得到它)。 ?我怎样才能做到这一点。

I need Class A to be accessible from the Classes library clientside (so that the custom controls can get to it). How can I do this?

我已经做到了这一点:

http://msdn.microsoft.com/en-us/library/ee707369%28v = vs.91%29.aspx

但* .shared.cs约定不给比实际的客户端库访问类其他图书馆第二种方法(添加为链接),并做我想做的事情,除了在服务器项目更新ClassA.cs不会导致客户端版本更新,因此有必要更新这两个类中的每个他们改变时间的文件,的这是不能接受

but the *.shared.cs convention doesn't give libraries other than the actual Client library access to Class A. The second method (Add as Link) does do what I want it to do, except that updating ClassA.cs in the server project doesn't cause the Client version to update, making it necessary to update both class files each time they're changed, which is unacceptable.

编辑:添加为链接再次尝试后几大工作次

推荐答案

在Visual Studio中(2010年,至少—正是不知道磨片添加了功能),您可以将现有项目到项目添加为链接,意思是源文件的项目之间共享。只有一个版本的源文件的存在。

In Visual Studio (2010, at least — dunno exactly whe the feature was added), you can add an existing item to a project as a 'link', meaning the source file is shared between projects. Only one version of the source file exists.


  1. 在您的项目中单击鼠标右键。

  2. 点击在Add..Existing项目。

  3. 找到选择的源文件,并选择它。

  4. 添加按钮是一个掇下。点击下拉图标的按钮。

  5. 单击添加为链接。

  1. Right-click on your project.
  2. Click on 'Add..Existing Item'.
  3. Find the source file of choice and select it.
  4. The 'Add' Button is a drop-down. Click the drop-down icon in the button.
  5. Click on 'Add As Link'.

简单!

现在任何改变共享源文件,反映在这两个地方。缺点当然是开发者获取没有迹象表明将变为共享源文件可能比她可能实现更广泛的影响。

Now any change to the share source file is reflected on both places. The drawback, of course, is that the developer gets no indication that changes to the shared source file might have wider ramifications than she might realize.

另一个选择是创建一个硬链接这样两个文件名引用(在Unix的术语索引节点)相同的文件。在命令行,只是歌颂魔法咒语:

Another option would be to create a hard link so two file names reference the same file ('inode' in Unix terms). On the command line, just chant the magic incantation:

fsutil hardlink create <new-filename> <existing-filename>



是这样的:

something like:

fsutil hardlink create c:\foo\bar\some-project\bazbat.cs c:\foo\bar\another-project\bazbat.cs

限制是这两个名称必须是同一个卷上。

The restriction is that both names have to be on the same volume.

这有可能,当然,混淆你的源代码控制系统。我敢打赌,TFS还没有考虑这种可能性,即文件系统不一定是树状结构和相同的文件可能会在多个目录存在。

This has the possibility, of course, confusing your source control system. I'm willing to bet that TFS hasn't consider the possibility that a filesystem isn't necessarily a tree structure and that the same file might exist in multiple directories.

硬链接的文件可以任何顺序被删除:文件停止时到获取删除最后一个环节存在

Hard-linked files may be deleted in any order: the file ceases to exist when the last link to gets removed.

第三选项,当然,并且很可能的最佳实践&MDASH;讨厌这个词! &MDASH;是将碎片类分解出成被部署到客户端和服务器的独立组件。如果想限制组件的漂浮数,生成后,您可以使用 ilmerge 合并组件:

The 3rd option, of course, and likely the "best-practice" — hate that term! — is to factor out the shard classes into an independent assembly that gets deployed to both the client and server. If want to limit the number of assemblies floating around, post-build, you can use ilmerge to merge assemblies:

  • http://www.codeproject.com/Articles/9364/Merging-NET-assemblies-using-ILMerge
  • Merge two assemblies at runtime - C#
  • How to: Merge multiple assemblies into one

试想想起来了,但也没有理由不能嵌入的共享组件作为获得按需加载的(甚至是在启动时)嵌入的资源。这甚至可能会比使用 ilmerge 清洁剂。以下是如何做到这一点:

Come to think of it, there's also no reason you can't embed the shared assemblies as embedded resources that get loaded on demand (or even at startup). This might even be cleaner than using ilmerge. Here's how to do that:

http://blogs.msdn.com/b/microsoft_press/archive/2010/02/03/jeffrey-里希特 - 摘录-2-从-CLR-通过-C-第三edition.aspx

这篇关于在Silverlight服务器和客户端项目之间共享类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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