简体   繁体   English

在多个项目之间共享实例

[英]Share an instance between multiple projects

I am working in VS 2008 C# and need to share an instance of an object created in one project with another project. 我正在VS 2008 C#中工作,需要与另一个项目共享在一个项目中创建的对象的实例。 I tried creating a static class in project1 and adding it as a link to project2, but the information wasn't saved. 我尝试在project1中创建一个静态类并将其添加为对project2的链接,但未保存信息。 The static class was written in project1. 静态类是在project1中编写的。

//object o = new object
//project1.staticObject = o
//project2.object = project1.staticObject

When I tried something like above, project2.object would be null. 当我尝试上述操作时,project2.object将为null。 By adding a class as a link, is it creating a new instance of the static class in project2 or is it referencing the same class? 通过添加一个类作为链接,它是在project2中创建静态类的新实例还是引用相同的类? If it is referencing the same class, shouldn't any information saved into the static class from project1 be accessible by project2? 如果它引用相同的类,那么project2是否不可以访问从project1保存到静态类中的任何信息? I know this isn't the most elegant manner of sharing data, but if anyone would help with this problem or provide a better manner of doing it, I would greatly appreciate it. 我知道这不是共享数据的最优雅的方式,但是如果有人可以帮助解决此问题或提供更好的方式进行共享,我将不胜感激。

Thanks in advance. 提前致谢。

Projects run in separate processes, so they can't share data in this manner. 项目在单独的流程中运行,因此它们不能以这种方式共享数据。 You'll need to persist the data in another type of store. 您需要将数据保留在另一种类型的存储中。 I recommend using a database (hey, 20 gazillion websites, stock trading apps, airlines, etc can't be wrong). 我建议使用数据库(嘿,有20个gazillion网站,股票交易应用,航空公司等都没错)。

If you don't want to use a database, you could open an IP connection between instances of the app and have a thread send packets of data to sync back and forth between the applications. 如果您不想使用数据库,则可以在应用程序实例之间打开IP连接,并让线程发送数据包以在应用程序之间来回同步。 Or, in your "server" app, add a web service that each process would call to update and retrieve information. 或者,在您的“服务器”应用程序中,添加一个Web服务,每个进程都将调用该服务来更新和检索信息。

If you need really high-speed communication between the processes, sockets with a peer or star topology is a good way to go. 如果您需要进程之间的真正高速通信,那么具有对等拓扑或星形拓扑的套接字是一个不错的选择。 If you're okay with some latency, having a web service (which works fine even if these aren't web apps) or a database could be a good solution. 如果您对某些延迟感到满意,那么拥有Web服务(即使这些不是Web应用程序也能正常工作)或数据库可能是一个不错的解决方案。 The right approach depends on your application. 正确的方法取决于您的应用程序。

WCF could also solve this problem. WCF也可以解决此问题。 It effectively wraps the IP/socket layer and provides some nice object persistence/remote control capabilities, but I find it overly complex for most applications. 它有效地包装了IP /套接字层,并提供了一些不错的对象持久性/远程控制功能,但是我发现对于大多数应用程序来说,它过于复杂。

To share a single instance of an object among different processes (that's what I think you are intending to do) you need something that will maintain that object's state. 要在不同进程之间共享一个对象的单个实例(这就是我想做的事情),您需要一些东西来维持该对象的状态。 You can look at the WCF and how to set up it's behaviour to act as a singleton so essentially every requester gets the same instance across the board. 您可以查看WCF以及如何设置其行为以使其成为单例,以便基本上每个请求者都能获得相同的实例。

http://msdn.microsoft.com/en-us/magazine/cc163590.aspx http://msdn.microsoft.com/zh-CN/magazine/cc163590.aspx

Creating the link creates only applies to the source code. 创建链接只会创建适用于源代码的链接。 When you compile each project, it then has that single class definition available in both projects. 当您编译每个项目时,然后在两个项目中都可以使用单个类定义。 The process you took does nothing for instances during runtime for sharing. 在共享期间,您执行的过程不会对实例产生任何影响。

You can look at WCF or .NET Remoting , although .NET Remoting is now officially replaced by WCF. 您可以查看WCF.NET Remoting ,尽管.NET Remoting现在已正式由WCF取代。

If you are talking about sharing the same object between two processes, you can do that, the concept is called memory-mapped files. 如果您正在谈论在两个进程之间共享同一对象,则可以做到这一点,这个概念称为内存映射文件。 Here is some starter docs from msdn. 这是msdn的一些入门文档。

Though the docs and API use the term "FileMapping" quite a bit, you can use it just for sharing memory between two processes. 尽管文档和API经常使用“ FileMapping”一词,但您可以将其仅用于在两个进程之间共享内存。

In .NET 4.0, you can use the System.IO.MemoryMappedFiles namespace. 在.NET 4.0中,可以使用System.IO.MemoryMappedFiles命名空间。 For your case, looks like .NET 3.5, you'll have to use some sort of interop to use the Win API. 对于您的情况,看起来像.NET 3.5,您必须使用某种互操作才能使用Win API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM