SynchronizationAttribute SynchronizationAttribute SynchronizationAttribute SynchronizationAttribute Class

定义

为当前上下文和所有共享同一实例的上下文强制一个同步域。Enforces a synchronization domain for the current context and all contexts that share the same instance.

public ref class SynchronizationAttribute : System::Runtime::Remoting::Contexts::ContextAttribute, System::Runtime::Remoting::Contexts::IContributeClientContextSink, System::Runtime::Remoting::Contexts::IContributeServerContextSink
[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
[System.Serializable]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeClientContextSink
    interface IContributeServerContextSink
Public Class SynchronizationAttribute
Inherits ContextAttribute
Implements IContributeClientContextSink, IContributeServerContextSink
继承
SynchronizationAttributeSynchronizationAttributeSynchronizationAttributeSynchronizationAttribute
属性
实现

示例

下面的代码示例演示如何使用SynchronizationAttributeThe following code example demonstrates the use of the SynchronizationAttribute. 有关完整的示例代码, 请参阅AsyncResult类的示例。For the complete example code, see the example for the AsyncResult class.

// Context-bound type with the Synchronization context attribute.

[Synchronization]
public ref class SampleSynchronized: public ContextBoundObject
{
public:

   // A method that does some work, and returns the square of the given number.
   int Square( int i )
   {
      Console::Write( "The hash of the thread executing " );
      Console::WriteLine( "SampleSynchronized::Square is: {0}", Thread::CurrentThread->GetHashCode() );
      return i * i;
   }

};
// Context-bound type with the Synchronization context attribute.
[Synchronization()]
public class SampleSynchronized : ContextBoundObject {

    // A method that does some work, and returns the square of the given number.
    public int Square(int i)  {

        Console.Write("The hash of the thread executing ");
        Console.WriteLine("SampleSynchronized.Square is: {0}", 
                             Thread.CurrentThread.GetHashCode());
        return i*i;
    }
}
' Context-bound type with the Synchronization context attribute.
<Synchronization()> Public Class SampleSynchronized
   Inherits ContextBoundObject
   
   ' A method that does some work, and returns the square of the given number.
   Public Function Square(i As Integer) As Integer
      
      Console.Write("The hash of the thread executing ")
      Console.WriteLine("SampleSynchronized.Square is: {0}", Thread.CurrentThread.GetHashCode())
      Return i * i
   End Function 

End Class 

注解

如果将此特性应用于对象, 则在共享此属性的实例的所有上下文中只能执行一个线程。When this attribute is applied to an object, only one thread can be executing in all contexts that share an instance of this property. 这是通过将截获和序列化各自上下文的传入调用的接收器来完成的。This is done by contributing sinks that intercept and serialize incoming calls for the respective contexts. 如果将属性标记为要重入, 则也会截获标注。If the property is marked for reentry, then callouts are intercepted too. 标注截取允许其他等待线程进入同步域以获得最大吞吐量。The callout interception allows other waiting threads to enter the synchronization domain for maximal throughput.

备注

有两个名为SynchronizationAttributeSystem.Runtime.Remoting.Contexts类: 一个位于命名空间中, 另一个System.EnterpriseServices位于命名空间中。There are two classes named SynchronizationAttribute : one in the System.Runtime.Remoting.Contexts namespace, and the other in the System.EnterpriseServices namespace. System.EnterpriseServices.SynchronizationAttribute类仅支持同步调用, 并且只能与服务组件一起使用。The System.EnterpriseServices.SynchronizationAttribute class supports only synchronous calls, and can be used only with serviced components. System.Runtime.Remoting.Contexts.SynchronizationAttribute支持同步和异步调用, 并且只能与上下文绑定对象一起使用。The System.Runtime.Remoting.Contexts.SynchronizationAttribute supports both synchronous and asynchronous calls, and can be used only with context bound objects. (有关上下文绑定对象的详细信息, 请参阅ContextBoundObject类。)(For more information on context bound objects, see the ContextBoundObject class.)

备注

此类在类级别建立链接要求和继承要求。This class makes a link demand and an inheritance demand at the class level. 当直接调用方或派生类没有基础结构权限时,将引发。SecurityExceptionA 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.

构造函数

SynchronizationAttribute() SynchronizationAttribute() SynchronizationAttribute() SynchronizationAttribute()

使用默认值初始化 SynchronizationAttribute 类的新实例。Initializes a new instance of the SynchronizationAttribute class with default values.

SynchronizationAttribute(Boolean) SynchronizationAttribute(Boolean) SynchronizationAttribute(Boolean) SynchronizationAttribute(Boolean)

用指示是否需要重入的布尔值来初始化 SynchronizationAttribute 类的新实例。Initializes a new instance of the SynchronizationAttribute class with a Boolean value indicating whether reentry is required.

SynchronizationAttribute(Int32) SynchronizationAttribute(Int32) SynchronizationAttribute(Int32) SynchronizationAttribute(Int32)

用指示该特性所应用于的对象的行为的标志来初始化 SynchronizationAttribute 类的新实例。Initializes a new instance of the SynchronizationAttribute class with a flag indicating the behavior of the object to which this attribute is applied.

SynchronizationAttribute(Int32, Boolean) SynchronizationAttribute(Int32, Boolean) SynchronizationAttribute(Int32, Boolean) SynchronizationAttribute(Int32, Boolean)

用指示该特性所应用于的对象的行为的标志和指示是否需要重入的布尔值来初始化 SynchronizationAttribute 类的新实例。Initializes a new instance of the SynchronizationAttribute class with a flag indicating the behavior of the object to which this attribute is applied, and a Boolean value indicating whether reentry is required.

字段

AttributeName AttributeName AttributeName AttributeName

指示上下文特性的名称。Indicates the name of the context attribute.

(Inherited from ContextAttribute)
NOT_SUPPORTED NOT_SUPPORTED NOT_SUPPORTED NOT_SUPPORTED

指示不能在具有同步的上下文中创建该特性所应用于的类。Indicates that the class to which this attribute is applied cannot be created in a context that has synchronization. 此字段为常数。This field is constant.

REQUIRED REQUIRED REQUIRED REQUIRED

指示必须在具有同步的上下文中创建该特性所应用于的类。Indicates that the class to which this attribute is applied must be created in a context that has synchronization. 此字段为常数。This field is constant.

REQUIRES_NEW REQUIRES_NEW REQUIRES_NEW REQUIRES_NEW

指示每次都必须在具有该同步属性的新实例的上下文中创建应用此特性的类。Indicates that the class to which this attribute is applied must be created in a context with a new instance of the synchronization property each time. 此字段为常数。This field is constant.

SUPPORTED SUPPORTED SUPPORTED SUPPORTED

指示应用此特性的类与该上下文是否有同步无关。Indicates that the class to which this attribute is applied is not dependent on whether the context has synchronization. 此字段为常数。This field is constant.

属性

IsReEntrant IsReEntrant IsReEntrant IsReEntrant

获取或设置指示是否需要重入的布尔值。Gets or sets a Boolean value indicating whether reentry is required.

Locked Locked Locked Locked

获取或设置指示实现此 Context 实例的 SynchronizationAttribute 是否被锁定的布尔值。Gets or sets a Boolean value indicating whether the Context implementing this instance of SynchronizationAttribute is locked.

Name Name Name Name

获取上下文特性的名称。Gets the name of the context attribute.

(Inherited from ContextAttribute)
TypeId TypeId TypeId TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。When implemented in a derived class, gets a unique identifier for this Attribute.

(Inherited from Attribute)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

返回一个布尔值,该值指示此实例是否与指定的对象相等。Returns a Boolean value indicating whether this instance is equal to the specified object.

(Inherited from ContextAttribute)
Freeze(Context) Freeze(Context) Freeze(Context) Freeze(Context)

当上下文冻结时调用。Called when the context is frozen.

(Inherited from ContextAttribute)
GetClientContextSink(IMessageSink) GetClientContextSink(IMessageSink) GetClientContextSink(IMessageSink) GetClientContextSink(IMessageSink)

创建一个标注接收器,并将其链接到所提供的位于远程处理调用客户端的上下文边界处的接收器链的前面。Creates a CallOut sink and chains it in front of the provided chain of sinks at the context boundary on the client end of a remoting call.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

返回此 ContextAttribute 实例的哈希代码。Returns the hashcode for this instance of ContextAttribute.

(Inherited from ContextAttribute)
GetPropertiesForNewContext(IConstructionCallMessage) GetPropertiesForNewContext(IConstructionCallMessage) GetPropertiesForNewContext(IConstructionCallMessage) GetPropertiesForNewContext(IConstructionCallMessage)

Synchronized 上下文属性添加到指定的 IConstructionCallMessageAdds the Synchronized context property to the specified IConstructionCallMessage.

GetServerContextSink(IMessageSink) GetServerContextSink(IMessageSink) GetServerContextSink(IMessageSink) GetServerContextSink(IMessageSink)

创建一个同步调度接收器,并将其链接到所提供的位于远程处理调用的服务器端的上下文边界处的接收器链的前面。Creates a synchronized dispatch sink and chains it in front of the provided chain of sinks at the context boundary on the server end of a remoting call.

GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
IsContextOK(Context, IConstructionCallMessage) IsContextOK(Context, IConstructionCallMessage) IsContextOK(Context, IConstructionCallMessage) IsContextOK(Context, IConstructionCallMessage)

返回一个指示该上下文参数是否满足上下文特性要求的布尔值。Returns a Boolean value indicating whether the context parameter meets the context attribute's requirements.

IsDefaultAttribute() IsDefaultAttribute() IsDefaultAttribute() 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)
IsNewContextOK(Context) IsNewContextOK(Context) IsNewContextOK(Context) IsNewContextOK(Context)

返回一个指示上下文属性是否与新上下文兼容的布尔值。Returns a Boolean value indicating whether the context property is compatible with the new context.

(Inherited from ContextAttribute)
Match(Object) Match(Object) Match(Object) Match(Object)

当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(Inherited from Attribute)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

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

将一组名称映射为对应的一组调度标识符。Maps a set of names to a corresponding set of dispatch identifiers.

(Inherited from Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。Retrieves the type information for an object, which can be used to get the type information for an interface.

(Inherited from Attribute)
_Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32) _Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(Inherited from Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。Provides access to properties and methods exposed by an object.

(Inherited from Attribute)

安全性

SecurityPermission
用于处理基础结构代码。for operating with infrastructure code. 要求值: InheritanceDemand;权限值:InfrastructureDemand value: InheritanceDemand; Permission Value: Infrastructure

适用于