[英]Cannot retrieve an int property over .NET remoting in C#
我在通過.NET遠程通道讀取屬性時遇到問題。 這是我班的開始:
[Serializable]
public class MachineID
{
private string mySystemDeviceSerial = null;
/// <summary>
/// Returns the hard drive serial that Windows is installed on.
/// </summary>
public string SystemDeviceSerial
{
get { return mySystemDeviceSerial; }
}
private string mySystemName = null;
/// <summary>
/// Returns the current name of the system.
/// </summary>
public string SystemName
{
get { return mySystemName; }
}
private string myLastError = string.Empty;
/// <summary>
/// Returns the last error that occurred. Returns string.Empty if no error has occurred.
/// </summary>
public string LastError
{
get { return myLastError; }
}
private int myPort = -2;
public int Port
{
get { return this.myPort; }
set { this.myPort = value; }
}
可以在本地計算機上訪問所有屬性。 但是,當我嘗試通過.NET遠程通道從客戶端讀取Port屬性時,我沒有獲得已分配給myPort的值,而是獲得了初始值-2。 我對此感到困惑,因為所有字符串屬性都可以很好地讀取。 有任何想法嗎? 我不能正確地序列化此類嗎?
值得一提的是,我所做的所有使類和成員可序列化的操作都是在類頂部添加[Serializable]屬性。 我不確定這是否是正確的方法,所以也許這可能是問題的一部分?
我的猜測(我強調這只是一個猜測)是您的訂單有誤-您是
使用[Serializable]
,會將對象轉換為字節流,通過電線將其傳輸到遠程客戶端,然后將其重構為客戶端上的新對象。 這意味着客戶端的副本已與服務器的副本完全斷開連接。 如果希望客戶端的副本始終顯示服務器中的更新值(而不重新傳輸整個對象),請使其像@ Hans-Passant所建議的那樣從MarshalByRefObject繼承。 這樣一來,客戶端就可以復制一個透明的代理,每次訪問服務器的一個屬性時,該代理就會查詢服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.