UrlAttribute 類別

定義

定義屬性 (Attribute),這屬性可用於呼叫站台以指定將發生啟動過程 (Activation) 的 URL。 此類別無法獲得繼承。

C#
[System.Serializable]
public sealed class UrlAttribute : System.Runtime.Remoting.Contexts.ContextAttribute
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class UrlAttribute : System.Runtime.Remoting.Contexts.ContextAttribute
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public sealed class UrlAttribute : System.Runtime.Remoting.Contexts.ContextAttribute
繼承
屬性

範例

下列程式碼範例說明如何在設定用戶端啟動遠端處理時使用 UrlAttribute 。 此範例包含三個部分:用戶端、伺服器,以及用戶端和伺服器所使用的遠端物件。

下列程式碼範例顯示用戶端:

C#
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class Client
{
    [STAThread]
    public static void Main()
    {
        // Report the initial status.
        Console.WriteLine("Starting client.");

        // Register the TCP channel.
        ChannelServices.RegisterChannel(new TcpChannel());

        // Create a url attribute object.
        UrlAttribute attribute =
            new UrlAttribute("tcp://localhost:1234/RemoteApp");
        Console.WriteLine("UrlAttribute value: {0}", attribute.UrlValue);
        object[] activationAttributes = new object[] { attribute };

        // Register the client for the remote object.
        RemotingConfiguration.RegisterActivatedClientType(
            typeof(RemoteObject),
            "tcp://localhost:1234/RemoteApp");

        // Activate the remote object.
        Console.WriteLine("Activating remote object.");
        RemoteObject obj = (RemoteObject) Activator.CreateInstance(
            typeof(RemoteObject), null, activationAttributes);

        // Invoke a method on the remote object.
        Console.WriteLine("Invoking Hello() on remote object.");
        obj.Hello();

        // Inform the user that the program is exiting.
        Console.WriteLine("The client is exiting.");
    }
}

下列程式碼範例顯示此用戶端的伺服器:

C#
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

public class Server
{
    [STAThread]
    public static void Main()
    {
        // Report the status to the user.
        Console.WriteLine("Starting server.");

        // Register the TCP channel.
        ChannelServices.RegisterChannel(new TcpChannel(1234));

        // Set the application name.
        RemotingConfiguration.ApplicationName = "RemoteApp";

        // Register the object for remoting.
        RemotingConfiguration.RegisterActivatedServiceType(
            typeof(RemoteObject));

        // Wait until the user presses ENTER.
        Console.WriteLine("Press ENTER to exit.");
        Console.ReadLine();
        Console.WriteLine("The server is exiting.");
    }
}

下列程式碼範例顯示用戶端和伺服器所使用的遠端物件:

C#
using System;
using System.Security;
using System.Security.Permissions;

public class RemoteObject : MarshalByRefObject
{
    public RemoteObject()
    {
        Console.WriteLine("You have called the constructor.");
    }

    public void Hello()
    {
        Console.WriteLine("You have called Hello().");
    }
}

備註

會在 UrlAttribute 使用 CreateInstance 方法建立啟動的物件時,將 啟用屬性陣列當做 參數傳遞至 Activator.CreateInstance

如需使用屬性的詳細資訊,請參閱 屬性

建構函式

UrlAttribute(String)

建立 UrlAttribute 類別的新執行個體。

欄位

AttributeName

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

表示內容屬性的名稱。

(繼承來源 ContextAttribute)

屬性

Name

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

取得內容屬性的名稱。

(繼承來源 ContextAttribute)
TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
UrlValue

取得 UrlAttribute 的 URL 值。

方法

Equals(Object)

檢查所指定物件是否參考到與目前執行個體相同的 URL。

Freeze(Context)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

在內容已凍結時呼叫。

(繼承來源 ContextAttribute)
GetHashCode()

傳回目前 UrlAttribute 的雜湊值 (Hash Value)。

GetPropertiesForNewContext(IConstructionCallMessage)

在指定的 URL 強制建立內容和內容中的伺服器物件。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsContextOK(Context, IConstructionCallMessage)

傳回布林 (Boolean) 值,指出指定的 Context 是否符合 UrlAttribute 的需求。

IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
IsNewContextOK(Context)

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

傳回布林值,指出內容屬性是否與新內容相容。

(繼承來源 ContextAttribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

另請參閱