如何跨进程共享类属性 [英] How to Share Class Properties Across Processes

查看:83
本文介绍了如何跨进程共享类属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚被问到如何在两个运行应用程序的
之间共享功能和属性。例如,我的App1和App2都运行在

相同的机器上。 App1使用包含函数SetProp()的DLL(可能)。

当App1调用它时,DLL中的属性设置为abc。 App2在同一个DLL中调用了一个

函数GetProp()。 GetProp()应该返回abc。


这听起来像一个简单的事情,但是让DLL变量共享

并使两个应用程序都加载相同的DLL不起作用。经过反思,

我可以看出为什么这不会(不应该)有效,但是怎么做呢;

最好不要跳过安检篮球。


TIA


Charles

解决方案

你好Charles Law,


您可以使用单身人士:


进口系统

Imports System.IO

Imports System.Collections

Imports System.Runtime.Serialization.Formatters.Binary

Imports System.Runtime.Serialization

''每个AppDomain应该只有一个这种类型的实例。

< Serializable()> Public NotInheritable Class Singleton

实现ISerializable


''这是这种类型的一个实例。

私有共享只读对象新单身人士


''以下是实例字段。

公共someString作为字符串

公共someNumber为Int32


''允许此类型构造Singleton的私有构造函数。

Private Sub New()

''做任何必要的事情来初始化Singleton。

someString ="这是一个字符串字段

someNumber = 123

End Sub


''返回对Singleton的引用的方法。

公共共享函数GetSingleton()As Singleton

返回theOneObject

结束函数


''序列化单例时调用的方法。

Private Sub GetObjectData(ByVal info As SerializationInfo,_

ByVal context作为Stre amingContext)_

实现ISerializable.GetObjectData


''而不是序列化这个对象,我们将

''序列化一个SingletonSerializationHelp相反。

info.SetType(GetType(SingletonSerializationHelper))

''不需要添加其他值。

End Sub


''注意:ISerializable'的特殊构造函数不是必需的

''因为它永远不会被调用。

End Class

< Serializable()>朋友NotInheritable类SingletonSerializationHelper

实现IObjectReference

''这个对象没有字段(虽然它可以)。


''GetRealObject在反序列化该对象后调用。

公共函数GetRealObject(ByVal context As StreamingContext)As

Object Implements IObjectReference.GetRealObject

''当取消这个对象,返回引用

''替换Singleton对象。

返回Singleton.GetSingleton()

结束函数

结束班

班级申请

< STAThread()> Shared Sub Main()

Dim fs As New FileStream(" DataFile.dat",FileMode.Create)


试试

''构造一个BinaryFormatter并使用它

''将数据序列化为流。

Dim formatter As New BinaryFormatter


''创建一个包含多个元素的数组,引用

''一个Singleton对象。

Dim a1()As Singleton = {Singleton.GetSingleton(),

Singleton.GetSingleton()}


''显示True。

Console.WriteLine("同时执行)数组元素引用同一个对象?

"& _

Object.ReferenceEquals(a1(0),a1(1)))


''序列化数组元素。

formatter.Serialize(fs,a1)


''反序列化数组元素。 />
fs.Position = 0

Dim a2()As Singleton = DirectCast(formatter.Deserialize(fs),

Singleton())


''显示True。

Console.WriteLine("两个数组元素是否指向同一个对象?

" &安培; _

Object.ReferenceEquals(a2(0),a2(1)))


''显示True。

Console.WriteLine("所有数组元素是否指向同一个对象?

"& _

Object.ReferenceEquals(a1(0),a2 (0)))

Catch e As SerializationException

Console.WriteLine(无法序列化。原因:& e.Message)

投掷

最后

fs.Close()

结束尝试

结束子

结束班


" Charles Law" < BL *** @ nowhere.com>在留言中写道

新闻:ec ************** @ TK2MSFTNGP15.phx.gbl ...

我刚被问到如何在两个正在运行的应用程序之间共享功能和属性。例如,我在同一台机器上运行
的App1和App2。 App1使用包含函数SetProp()的DLL(可能)。
当App1调用它时,DLL中的属性设置为abc。 App2在同一个DLL中调用一个
函数GetProp()。 GetProp()应该返回abc。

这听起来像一个简单的事情,但是使DLL变量共享
并使两个应用程序加载相同的DLL不起作用。在
反思中,我可以看出为什么这不会(不应该)起作用,但是如何才能完成;
最好不必跳过安全箍。

TIA

Charles



嗨Pipo


谢谢为了快速反应。您的示例似乎在

单个应用程序域中运行,而我需要在app域中运行。我已经在我的场景中尝试了你的例子,但是当我在第一个

应用程序中设置someString时,它在我的第二个应用程序中没有出现设置。


Charles

" Pipo" < P1 ** @ nobody.com>在消息中写道

新闻:O8 ************** @ TK2MSFTNGP12.phx.gbl ...

嗨Charles Law,

您可以使用Singleton:

进口系统
Imports System.IO
Imports System.Collections
Imports System.Runtime。 Serialization.Formatters.Binary
Imports System.Runtime.Serialization

''每个AppDomain应该只有一个这种类型的实例。
< Serializable()>公共NotInheritable类Singleton
实现ISerializable

''这是这种类型的一个实例。
私有共享ReadOnly theOneObject作为新的Singleton

''以下是实例字段。
公共someString As String
公共someNumber为Int32
''私有构造函数允许此类型构造Singleton。
Private Sub New( )
''做初始化Singleton所需的一切。
someString ="这是一个字符串字段
someNumber = 123
End Sub
''返回对Singleton的引用的方法。
公共共享函数GetSingleton()As Singleton
返回一个对象
结束函数

''一个方法调用时串行化Singleton。
Private Sub GetObjectData(ByVal info As SerializationInfo,_
ByVal context As StreamingContext)_
实现ISerializ able.GetObjectData

''而不是序列化这个对象,我们将'>序列化一个SingletonSerializationHelp。
info.SetType(GetType(SingletonSerializationHelper))
' '不需要添加其他值。
End Sub

''注意:ISerializable'的特殊构造函数不是必需的,因为它永远不会被调用。 />结束类

< Serializable()> Friend NotInheritable Class SingletonSerializationHelper
实现IObjectReference
''这个对象没有字段(虽然可以)。

''反序列化后调用GetRealObject。
公共函数GetRealObject(ByVal context As StreamingContext)As
Object Implements IObjectReference.GetRealObject
''当取消激活此对象时,返回对单个对象的引用。
返回Singleton.GetSingleton()
结束功能
结束类

Class App
< STAThread()> Shared Sub Main()
Dim fs As New FileStream(" DataFile.dat",FileMode.Create)

尝试
''构造BinaryFormatter并使用它
''将数据序列化为流。
Dim formatter As New BinaryFormatter

''创建一个包含多个元素的数组,引用
''一个Singleton对象。
Dim a1()As Singleton = {Singleton.GetSingleton(),
Singleton.GetSingleton()}
''这显示True。
控制台。 WriteLine(两个数组元素都指向相同的
对象?
& _
Object.ReferenceEquals(a1(0),a1(1)))

''序列化数组元素。
formatter.Serialize(fs,a1)

''反序列化数组元素。
fs.Position = 0 Dim a2()As Singleton = DirectCast(formatter.Deserialize(fs),
Singleton())

''显示True。
Console.WriteLine("两个数组元素是否同一个
对象?
&安培; _
Object.ReferenceEquals(a2(0),a2(1)))
''这显示True。
Console.WriteLine(" Do all array元素引用相同的对象?
"& _
Object.ReferenceEquals(a1(0),a2(0)))
Catch e As SerializationException
Console.WriteLine (序列化失败。原因:& e.Message
投掷
最后
fs.Close()
结束尝试
结束子
查尔斯法律 < BL *** @ nowhere.com>在消息中写道
新闻:ec ************** @ TK2MSFTNGP15.phx.gbl ...

我刚被问到如何分享功能和两个正在运行的应用程序之间的属性。例如,我有App1和App2都运行在


相同的机器上。 App1使用包含函数SetProp()的DLL(可能)。
当App1调用它时,DLL中的属性设置为abc。 App2在同一个DLL中调用一个
函数GetProp()。 GetProp()应该返回abc。

这听起来像一个简单的事情,但是使DLL变量共享
并使两个应用程序加载相同的DLL不起作用。在


反射,

我可以看到为什么那不会(不应该)起作用,但是怎么做呢;
最好没有跳过安全篮。

TIA




Charles,


您是否正在发明自己的远程风格?


Cor


I have just been asked how to share functions and properties between two
running applications. For example, I have App1 and App2 both running on the
same machine. App1 uses a DLL (perhaps) that contains function SetProp().
When App1 calls it, a property in the DLL is set to "abc". App2 calls a
function GetProp(), in the same DLL. GetProp() should return "abc".

This sounds like a simple thing to do, but making the DLL variable shared
and making both applications load the same DLL does not work. On reflection,
I can see why that wouldn''t (shouldn''t) work, but how can it be done;
preferably without having to jump through security hoops.

TIA

Charles

解决方案

Hi Charles Law,

You could use a Singleton for this:

Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
'' There should be only one instance of this type per AppDomain.
<Serializable()> Public NotInheritable Class Singleton
Implements ISerializable

'' This is the one instance of this type.
Private Shared ReadOnly theOneObject As New Singleton

'' Here are the instance fields.
Public someString As String
Public someNumber As Int32

'' Private constructor allowing this type to construct the Singleton.
Private Sub New()
'' Do whatever is necessary to initialize the Singleton.
someString = "This is a string field"
someNumber = 123
End Sub

'' A method returning a reference to the Singleton.
Public Shared Function GetSingleton() As Singleton
Return theOneObject
End Function

'' A method called when serializing a Singleton.
Private Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext) _
Implements ISerializable.GetObjectData

'' Instead of serializing this object, we will
'' serialize a SingletonSerializationHelp instead.
info.SetType(GetType(SingletonSerializationHelper) )
'' No other values need to be added.
End Sub

'' Note: ISerializable''s special constructor is not necessary
'' because it is never called.
End Class
<Serializable()> Friend NotInheritable Class SingletonSerializationHelper
Implements IObjectReference
'' This object has no fields (although it could).

'' GetRealObject is called after this object is deserialized.
Public Function GetRealObject(ByVal context As StreamingContext) As
Object Implements IObjectReference.GetRealObject
'' When deserialiing this object, return a reference to
'' the Singleton object instead.
Return Singleton.GetSingleton()
End Function
End Class
Class App
<STAThread()> Shared Sub Main()
Dim fs As New FileStream("DataFile.dat", FileMode.Create)

Try
'' Construct a BinaryFormatter and use it
'' to serialize the data to the stream.
Dim formatter As New BinaryFormatter

'' Create an array with multiple elements refering to
'' the one Singleton object.
Dim a1() As Singleton = {Singleton.GetSingleton(),
Singleton.GetSingleton()}

'' This displays "True".
Console.WriteLine("Do both array elements refer to the same object?
" & _
Object.ReferenceEquals(a1(0), a1(1)))

'' Serialize the array elements.
formatter.Serialize(fs, a1)

'' Deserialize the array elements.
fs.Position = 0
Dim a2() As Singleton = DirectCast(formatter.Deserialize(fs),
Singleton())

'' This displays "True".
Console.WriteLine("Do both array elements refer to the same object?
" & _
Object.ReferenceEquals(a2(0), a2(1)))

'' This displays "True".
Console.WriteLine("Do all array elements refer to the same object?
" & _
Object.ReferenceEquals(a1(0), a2(0)))
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
End Class

"Charles Law" <bl***@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...

I have just been asked how to share functions and properties between two
running applications. For example, I have App1 and App2 both running on the same machine. App1 uses a DLL (perhaps) that contains function SetProp().
When App1 calls it, a property in the DLL is set to "abc". App2 calls a
function GetProp(), in the same DLL. GetProp() should return "abc".

This sounds like a simple thing to do, but making the DLL variable shared
and making both applications load the same DLL does not work. On reflection, I can see why that wouldn''t (shouldn''t) work, but how can it be done;
preferably without having to jump through security hoops.

TIA

Charles



Hi Pipo

Thanks for the quick response. Your example appears to operate within a
single app domain, whereas I need this to operate across app domains. I have
tried you example in my scenario, but when I set someString in the first
application, it does not appear set in my second application.

Charles
"Pipo" <Pi**@nobody.com> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...

Hi Charles Law,

You could use a Singleton for this:

Imports System
Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
'' There should be only one instance of this type per AppDomain.
<Serializable()> Public NotInheritable Class Singleton
Implements ISerializable

'' This is the one instance of this type.
Private Shared ReadOnly theOneObject As New Singleton

'' Here are the instance fields.
Public someString As String
Public someNumber As Int32

'' Private constructor allowing this type to construct the Singleton.
Private Sub New()
'' Do whatever is necessary to initialize the Singleton.
someString = "This is a string field"
someNumber = 123
End Sub

'' A method returning a reference to the Singleton.
Public Shared Function GetSingleton() As Singleton
Return theOneObject
End Function

'' A method called when serializing a Singleton.
Private Sub GetObjectData(ByVal info As SerializationInfo, _
ByVal context As StreamingContext) _
Implements ISerializable.GetObjectData

'' Instead of serializing this object, we will
'' serialize a SingletonSerializationHelp instead.
info.SetType(GetType(SingletonSerializationHelper) )
'' No other values need to be added.
End Sub

'' Note: ISerializable''s special constructor is not necessary
'' because it is never called.
End Class
<Serializable()> Friend NotInheritable Class SingletonSerializationHelper
Implements IObjectReference
'' This object has no fields (although it could).

'' GetRealObject is called after this object is deserialized.
Public Function GetRealObject(ByVal context As StreamingContext) As
Object Implements IObjectReference.GetRealObject
'' When deserialiing this object, return a reference to
'' the Singleton object instead.
Return Singleton.GetSingleton()
End Function
End Class
Class App
<STAThread()> Shared Sub Main()
Dim fs As New FileStream("DataFile.dat", FileMode.Create)

Try
'' Construct a BinaryFormatter and use it
'' to serialize the data to the stream.
Dim formatter As New BinaryFormatter

'' Create an array with multiple elements refering to
'' the one Singleton object.
Dim a1() As Singleton = {Singleton.GetSingleton(),
Singleton.GetSingleton()}

'' This displays "True".
Console.WriteLine("Do both array elements refer to the same
object?
" & _
Object.ReferenceEquals(a1(0), a1(1)))

'' Serialize the array elements.
formatter.Serialize(fs, a1)

'' Deserialize the array elements.
fs.Position = 0
Dim a2() As Singleton = DirectCast(formatter.Deserialize(fs),
Singleton())

'' This displays "True".
Console.WriteLine("Do both array elements refer to the same
object?
" & _
Object.ReferenceEquals(a2(0), a2(1)))

'' This displays "True".
Console.WriteLine("Do all array elements refer to the same object?
" & _
Object.ReferenceEquals(a1(0), a2(0)))
Catch e As SerializationException
Console.WriteLine("Failed to serialize. Reason: " & e.Message)
Throw
Finally
fs.Close()
End Try
End Sub
End Class

"Charles Law" <bl***@nowhere.com> wrote in message
news:ec**************@TK2MSFTNGP15.phx.gbl...

I have just been asked how to share functions and properties between two
running applications. For example, I have App1 and App2 both running on


the

same machine. App1 uses a DLL (perhaps) that contains function SetProp().
When App1 calls it, a property in the DLL is set to "abc". App2 calls a
function GetProp(), in the same DLL. GetProp() should return "abc".

This sounds like a simple thing to do, but making the DLL variable shared
and making both applications load the same DLL does not work. On


reflection,

I can see why that wouldn''t (shouldn''t) work, but how can it be done;
preferably without having to jump through security hoops.

TIA

Charles




Charles,

Are you inventing your own style of remoting?

Cor


这篇关于如何跨进程共享类属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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