2010-02-08 7 views
40

Was macht MethodImplOptions.Synchronized?Was macht MethodImplOptions.Synchronized?

Ist der Code unter

[MethodImpl(MethodImplOptions.Synchronized)] 
public void Method() 
{ 
    MethodImpl(); 
} 

entspricht

public void Method() 
{ 
    lock(this) 
    { 
     MethodImpl(); 
    } 
} 

Antwort

38

Das von Mr. Jon Skeet auf einem anderen site beantwortet wurde.

Zitat von Post

Es ist das Äquivalent Schloss (das) um den gesamten Methodenaufruf zu setzen.

Der Post hat mehr Beispielcode.

+5

die zum Beispiel Methoden und Eigenschaften wahr ist, aber für statische diejenigen, sperrt sie von der Art, die [geht gegen Best-Practice ] (http://michaelprimeaux.com/blog/2008/01/09/methodimploptions-dot-synchronized/) und hat tatsächlich einen bekannten [Fehler auf x64-Systemen] (https://connect.microsoft.com/VisualStudio/feedback/details/423280/methodimpl-methodimploptions-synchronisierte-intermittierend-throws-synchronizationlockexception-when-used-on-statisch-method-on-x64) th at causes Objektsynchronisationsausnahmen werden ausgelöst. – Shaun