簡體   English   中英

無法在C#中通過.NET遠程檢索int屬性

[英]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]屬性。 我不確定這是否是正確的方法,所以也許這可能是問題的一部分?

我的猜測(我強調這只是一個猜測)是您的訂單有誤-您是

  1. 從遠程主機檢索項目
  2. 更新它(在主機上),然后
  3. 期望客戶端中的值將更新為您在主機上更改的值。

使用[Serializable] ,會將對象轉換為字節流,通過電線將其傳輸到遠程客戶端,然后將其重構為客戶端上的新對象。 這意味着客戶端的副本已與服務器的副本完全斷開連接。 如果希望客戶端的副本始終顯示服務器中的更新值(而不重新傳輸整個對象),請使其像@ Hans-Passant所建議的那樣從MarshalByRefObject繼承。 這樣一來,客戶端就可以復制一個透明的代理,每次訪問服務器的一個屬性時,該代理就會查詢服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM