ProxyAttribute Class
 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Indicates that an object type requires a custom proxy.

Namespace:   System.Runtime.Remoting.Proxies
Assembly:  mscorlib (in mscorlib.dll)

SystemObject
   SystemAttribute
    System.Runtime.Remoting.ProxiesProxyAttribute

[SecurityCriticalAttribute]
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false, 
	Inherited = true)]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, 
	Flags = SecurityPermissionFlag.Infrastructure)]
public class ProxyAttribute : Attribute, IContextAttribute
[SecurityCriticalAttribute]
[AttributeUsageAttribute(AttributeTargets::Class, AllowMultiple = false, 
	Inherited = true)]
[ComVisibleAttribute(true)]
[SecurityPermissionAttribute(SecurityAction::InheritanceDemand, 
	Flags = SecurityPermissionFlag::Infrastructure)]
public ref class ProxyAttribute : Attribute, IContextAttribute
[<SecurityCriticalAttribute>]
[<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = false,
    Inherited = true)>]
[<ComVisibleAttribute(true)>]
[<SecurityPermissionAttribute(SecurityAction.InheritanceDemand,
    Flags = SecurityPermissionFlag.Infrastructure)>]
type ProxyAttribute = 
    class
        inherit Attribute
        interface IContextAttribute
    end
<SecurityCriticalAttribute>
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := False,
	Inherited := True)>
<ComVisibleAttribute(True)>
<SecurityPermissionAttribute(SecurityAction.InheritanceDemand,
	Flags := SecurityPermissionFlag.Infrastructure)>
Public Class ProxyAttribute
	Inherits Attribute
	Implements IContextAttribute
NameDescription
System_CAPS_pubmethod ProxyAttribute

Initializes a new instance of the ProxyAttribute class with default values.

NameDescription
System_CAPS_pubproperty

When implemented in a derived class, gets a unique identifier for this Attribute.(Inherited from Attribute.)

NameDescription
System_CAPS_pubmethod CreateInstance

Creates either an uninitialized MarshalByRefObject or a transparent proxy, depending on whether the specified type can exist in the current context.

System_CAPS_pubmethod CreateProxy

Creates an instance of a remoting proxy for a remote object described by the specified ObjRef, and located on the server.

System_CAPS_pubmethod Equals

This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode

Returns the hash code for this instance.(Inherited from Attribute.)

System_CAPS_pubmethod GetPropertiesForNewContext

Gets properties for a new context.

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethod IsContextOK

Checks the specified context.

System_CAPS_pubmethod IsDefaultAttribute

When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.(Inherited from Attribute.)

System_CAPS_pubmethod Match

When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetIDsOfNames

Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfo

Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeGetTypeInfoCount

Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.)

System_CAPS_pubinterface System_CAPS_privmethod _AttributeInvoke

Provides access to properties and methods exposed by an object.(Inherited from Attribute.)

Apply the current attribute to types that need custom proxies. You can use the ProxyAttribute class to intercept the new (New in Visual Basic) statement by deriving from the ProxyAttribute and placing the attribute on a child of ContextBoundObject. (Placing the proxy attribute on a child of MarshalByRefObject is not supported.)

System_CAPS_noteNote

This class makes a link demand and an inheritance demand at the class level. A SecurityException is thrown when either the immediate caller or the derived class does not have infrastructure permission. For details about security demands, see Link Demands and Inheritance Demands.

[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.Infrastructure)]
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;
   }
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[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 'NewNew

   ' 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

<MyProxyAttribute()> _
Public Class CustomServer
   Inherits ContextBoundObject

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

   Public Sub HelloMethod(str As String)
      Console.WriteLine("HelloMethod of Server is invoked with message : " + str)
   End Sub 'HelloMethod
End Class 'CustomServer
[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 );
   }
};
SecurityPermission

For operating with infrastructure code. Demand value: SecurityActionLinkDemand; Permission Value: SecurityPermissionFlagInfrastructure

SecurityPermission

For operating with infrastructure code. Demand value: SecurityActionInheritanceDemand; Permission Value: SecurityPermissionFlagInfrastructure

.NET Framework
Available since 1.1

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top