反映私有远程对象

发布于 2024-07-26 04:32:38 字数 452 浏览 3 评论 0原文

我想从远程对象返回私有字段,但出现异常:

RemotingException was returned 远程处理无法在类型“DBGeneral”上找到字段“connectionString”。

执行 GetField() 方法时,我确实获取了 Private 字段的 FieldInfo 对象。

FieldInfo field = o.GetType().GetField("connectionString", BindingFlags.Instance | BindingFlags.NonPublic);

但是当执行GetValue()时,会抛出RemotingException。

field.GetValue(o);

如果我关闭远程处理并反映本地私有连接字符串字段,我会得到返回给我的字符串。

I want to return a private field from a remoted object but i get the exception:

RemotingException was caught
Remoting cannot find field 'connectionString' on type 'DBGeneral'.

I DO get the Private field's FieldInfo object when executing the GetField() method.

FieldInfo field = o.GetType().GetField("connectionString", BindingFlags.Instance | BindingFlags.NonPublic);

But its when execute GetValue(), that the RemotingException is thrown.

field.GetValue(o);

If i turn remoting off and reflect the local private connectionString field, i get the string returned to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-08-02 04:32:38

这对您如何远程处理做出了一些假设,可能不正确,但可以解释您的错误。

当您的对象通过远程处理发送时,它必须被序列化。 序列化程序只能“查看”公共属性,因此私有属性不会通过线路发送。

编辑:基于评论

您至少有两个选择:

最简单的一个是将财产公开。

一项需要做更多工作的工作是从使用远程处理切换到使用 WCF。 然后你可以像这样标记你的私有变量:

[DataMember(Name="SomeValue")]
private int m_SomeValue;

This makes some assumptions about how you are remoting, may not be right but would explain your error.

When your object is sent over remoting it must be serialized. The serializer can only "see" the public properties, therefore private properties do not get sent over the wire.

Edit: Based on Comment

You have atleast 2 options:

The simple one is to make the property public.

One that is much more work is to switch from using remoting to using WCF. Then you can mark your private variable like this:

[DataMember(Name="SomeValue")]
private int m_SomeValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文