ProxyAttribute 類別

定義

指示物件型別需要自訂 Proxy。

public ref class ProxyAttribute : Attribute, System::Runtime::Remoting::Contexts::IContextAttribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
public class ProxyAttribute : Attribute, System.Runtime.Remoting.Contexts.IContextAttribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ComVisible(true)]
public class ProxyAttribute : Attribute, System.Runtime.Remoting.Contexts.IContextAttribute
[System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public class ProxyAttribute : Attribute, System.Runtime.Remoting.Contexts.IContextAttribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)>]
type ProxyAttribute = class
    inherit Attribute
    interface IContextAttribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ProxyAttribute = class
    inherit Attribute
    interface IContextAttribute
[<System.AttributeUsage(System.AttributeTargets.Class, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type ProxyAttribute = class
    inherit Attribute
    interface IContextAttribute
Public Class ProxyAttribute
Inherits Attribute
Implements IContextAttribute
繼承
ProxyAttribute
屬性
實作

範例

[AttributeUsageAttribute(AttributeTargets::Class)]
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::LinkDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::InheritanceDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::Infrastructure)]
public ref class MyProxyAttribute: public ProxyAttribute
{
public:
   MyProxyAttribute(){}

   // Create an instance of ServicedComponentProxy
   virtual MarshalByRefObject^ CreateInstance( Type^ serverType ) override
   {
      return ProxyAttribute::CreateInstance( serverType );
   }

   virtual RealProxy^ CreateProxy( ObjRef^ objRef1, Type^ serverType, Object^ serverObject, Context^ serverContext ) override
   {
      MyProxy^ myCustomProxy = gcnew MyProxy( serverType );
      if ( serverContext != nullptr )
      {
         RealProxy::SetStubData( myCustomProxy, serverContext );
      }

      if ( ( !serverType->IsMarshalByRef) && (serverContext == nullptr) )
      {
         throw gcnew RemotingException( "Bad Type for CreateProxy" );
      }

      return myCustomProxy;
   }
};

[MyProxyAttribute]
ref class CustomServer: public ContextBoundObject
{
public:
   CustomServer()
   {
      Console::WriteLine( "CustomServer Base Class constructor called" );
   }

   void HelloMethod( String^ str )
   {
      Console::WriteLine( "HelloMethod of Server is invoked with message : {0}", str );
   }
};
[AttributeUsage(AttributeTargets.Class)]
public class MyProxyAttribute : ProxyAttribute
{
   public MyProxyAttribute()
   {
   }
   // Create an instance of ServicedComponentProxy
   public override MarshalByRefObject CreateInstance(Type serverType)
   {
      return base.CreateInstance(serverType);
   }
   public override RealProxy CreateProxy(ObjRef objRef1,
      Type serverType,
      object serverObject,
      Context serverContext)
   {
      MyProxy myCustomProxy = new MyProxy(serverType);
      if(serverContext != null)
      {
         RealProxy.SetStubData(myCustomProxy,serverContext);
      }
      if((!serverType.IsMarshalByRef)&&(serverContext == null))
      {
         throw new RemotingException("Bad Type for CreateProxy");
      }
      return myCustomProxy;
   }
}
[MyProxyAttribute]
public class CustomServer :ContextBoundObject
{
   public CustomServer()
   {
      Console.WriteLine("CustomServer Base Class constructor called");
   }
   public void HelloMethod(string str)
   {
      Console.WriteLine("HelloMethod of Server is invoked with message : " + str);
   }
}
<SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.Infrastructure), _
AttributeUsage(AttributeTargets.Class)>  _
Public Class MyProxyAttribute
   Inherits ProxyAttribute

   Public Sub New()
   End Sub

   ' Create an instance of ServicedComponentProxy
   Public Overrides Function CreateInstance(serverType As Type) As MarshalByRefObject
      Return MyBase.CreateInstance(serverType)
   End Function 'CreateInstance

   Public Overrides Function CreateProxy(objRef1 As ObjRef, serverType As Type, _
               serverObject As Object, serverContext As Context) As RealProxy
      Dim myCustomProxy As New MyProxy(serverType)
      If Not (serverContext Is Nothing) Then
         RealProxy.SetStubData(myCustomProxy, serverContext)
      End If
      If Not serverType.IsMarshalByRef And serverContext Is Nothing Then
         Throw New RemotingException("Bad Type for CreateProxy")
      End If
      Return myCustomProxy
   End Function 'CreateProxy
End Class

<MyProxyAttribute()> _
Public Class CustomServer
   Inherits ContextBoundObject

   Public Sub New()
      Console.WriteLine("CustomServer Base Class constructor called")
   End Sub

   Public Sub HelloMethod(str As String)
      Console.WriteLine("HelloMethod of Server is invoked with message : " + str)
   End Sub
End Class

備註

將目前的屬性套用至需要自訂 Proxy 的類型。 您可以使用 ProxyAttribute 類別來攔截 new Visual Basic) 語句中的 (New ,方法是衍生自 ProxyAttribute 並將 屬性放在 的 ContextBoundObject 子系上。 (不支援將 Proxy 屬性放在 的 MarshalByRefObject 子系上。)

注意

這個類別會在類別層級建立連結需求和繼承需求。 SecurityException當立即呼叫端或衍生類別沒有基礎結構許可權時,就會擲回 。 如需安全性需求的詳細資訊,請參閱 連結需求繼承需求

建構函式

ProxyAttribute()

使用預設值,初始化 ProxyAttribute 類別的新執行個體。

屬性

TypeId

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

(繼承來源 Attribute)

方法

CreateInstance(Type)

建立未初始化的 MarshalByRefObject 或者透明 Proxy,依指定型別是否可以在目前內容中存在而定。

CreateProxy(ObjRef, Type, Object, Context)

ObjRef 所描述而位於伺服器上的遠端物件建立遠端 Proxy 的執行個體。

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetPropertiesForNewContext(IConstructionCallMessage)

取得新內容的屬性。

GetType()

取得目前執行個體的 Type

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

檢查指定的內容。

IsDefaultAttribute()

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

(繼承來源 Attribute)
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)

適用於

另請參閱