SynchronizationAttribute 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为当前上下文和所有共享同一实例的上下文强制一个同步域。
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.Serializable]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink[System.AttributeUsage(System.AttributeTargets.Class)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Security.SecurityCritical]
public class SynchronizationAttribute : System.Runtime.Remoting.Contexts.ContextAttribute, System.Runtime.Remoting.Contexts.IContributeClientContextSink, System.Runtime.Remoting.Contexts.IContributeServerContextSink[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSink[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSink[<System.AttributeUsage(System.AttributeTargets.Class)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Security.SecurityCritical>]
type SynchronizationAttribute = class
    inherit ContextAttribute
    interface IContributeServerContextSink
    interface IContributeClientContextSinkPublic Class SynchronizationAttribute
Inherits ContextAttribute
Implements IContributeClientContextSink, IContributeServerContextSink- 继承
- 属性
- 实现
示例
下面的代码示例演示如何使用 SynchronizationAttribute。 有关完整的示例代码,请参阅 类的示例 AsyncResult 。
// 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
注解
将此属性应用于对象时,在共享此属性实例的所有上下文中只能执行一个线程。 这是通过提供接收器来完成的,这些接收器截获和序列化相应上下文的传入调用。 如果属性标记为重新进入,则标注也会被截获。 标注截获允许其他等待线程进入同步域,以获取最大吞吐量。
注意
有两个名为 SynchronizationAttribute 的类:一个在 System.Runtime.Remoting.Contexts 命名空间中,另一个在命名空间中 System.EnterpriseServices 。 类 System.EnterpriseServices.SynchronizationAttribute 仅支持同步调用,并且只能与服务组件一起使用。 
              System.Runtime.Remoting.Contexts.SynchronizationAttribute支持同步调用和异步调用,并且只能与上下文绑定对象一起使用。  (有关上下文绑定对象的详细信息,请参阅 ContextBoundObject class.) 
注意
此类在类级别发出链接需求和继承需求。 SecurityException当直接调用方或派生类没有基础结构权限时,将引发 。 有关安全要求的详细信息,请参阅 链接需求 和 继承需求。
构造函数
| SynchronizationAttribute() | 使用默认值初始化 SynchronizationAttribute 类的新实例。 | 
| SynchronizationAttribute(Boolean) | 用指示是否需要重入的布尔值来初始化 SynchronizationAttribute 类的新实例。 | 
| SynchronizationAttribute(Int32) | 用指示该特性所应用于的对象的行为的标志来初始化 SynchronizationAttribute 类的新实例。 | 
| SynchronizationAttribute(Int32, Boolean) | 用指示该特性所应用于的对象的行为的标志和指示是否需要重入的布尔值来初始化 SynchronizationAttribute 类的新实例。 | 
字段
| AttributeName | 此 API 支持产品基础结构,不能在代码中直接使用。 指示上下文特性的名称。(继承自 ContextAttribute) | 
| NOT_SUPPORTED | 指示不能在具有同步的上下文中创建该特性所应用于的类。 此字段为常数。 | 
| REQUIRED | 指示必须在具有同步的上下文中创建该特性所应用于的类。 此字段为常数。 | 
| REQUIRES_NEW | 指示每次都必须在具有该同步属性的新实例的上下文中创建应用此特性的类。 此字段为常数。 | 
| SUPPORTED | 指示应用此特性的类与该上下文是否有同步无关。 此字段为常数。 | 
属性
| IsReEntrant | 获取或设置指示是否需要重入的布尔值。 | 
| Locked | 获取或设置指示实现此 Context 实例的 SynchronizationAttribute 是否被锁定的布尔值。 | 
| Name | 此 API 支持产品基础结构,不能在代码中直接使用。 获取上下文特性的名称。(继承自 ContextAttribute) | 
| TypeId | 在派生类中实现时,获取此 Attribute 的唯一标识符。(继承自 Attribute) | 
方法
| Equals(Object) | 此 API 支持产品基础结构,不能在代码中直接使用。 返回一个布尔值,该值指示此实例是否与指定的对象相等。(继承自 ContextAttribute) | 
| Freeze(Context) | 此 API 支持产品基础结构,不能在代码中直接使用。 当上下文冻结时调用。(继承自 ContextAttribute) | 
| GetClientContextSink(IMessageSink) | 创建一个标注接收器,并将其链接到所提供的位于远程处理调用客户端的上下文边界处的接收器链的前面。 | 
| GetHashCode() | 此 API 支持产品基础结构,不能在代码中直接使用。 返回此 ContextAttribute 实例的哈希代码。(继承自 ContextAttribute) | 
| GetPropertiesForNewContext(IConstructionCallMessage) | 将  | 
| GetServerContextSink(IMessageSink) | 创建一个同步调度接收器,并将其链接到所提供的位于远程处理调用的服务器端的上下文边界处的接收器链的前面。 | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| IsContextOK(Context, IConstructionCallMessage) | 返回一个指示该上下文参数是否满足上下文特性要求的布尔值。 | 
| IsDefaultAttribute() | 在派生类中重写时,指示此实例的值是否是派生类的默认值。(继承自 Attribute) | 
| IsNewContextOK(Context) | 此 API 支持产品基础结构,不能在代码中直接使用。 返回一个指示上下文属性是否与新上下文兼容的布尔值。(继承自 ContextAttribute) | 
| Match(Object) | 当在派生类中重写时,返回一个指示此实例是否等于指定对象的值。(继承自 Attribute) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| ToString() | 返回表示当前对象的字符串。(继承自 Object) | 
显式接口实现
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | 将一组名称映射为对应的一组调度标识符。(继承自 Attribute) | 
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | 检索对象的类型信息,然后可以使用该信息获取接口的类型信息。(继承自 Attribute) | 
| _Attribute.GetTypeInfoCount(UInt32) | 检索对象提供的类型信息接口的数量(0 或 1)。(继承自 Attribute) | 
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | 提供对某一对象公开的属性和方法的访问。(继承自 Attribute) |