Gendarme.Rules.Performance.AvoidLocalDataStoreSlotRule Class
This rule warns if a method use LocalDataStoreSlot to store or retrieve data from Thread or Context Local Storage. The faster alternative is to use [ThreadStatic] or [ContextStatic] attributes to avoid extra calls and typecasts. Also [ThreadStatic] is available on Silverlight while the LocalDataStoreSlot API are not.

See Also: AvoidLocalDataStoreSlotRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.Problem("LocalDataStoreSlot is slower than the [ThreadStatic] or [ContextStatic] alternatives.")]
[Gendarme.Framework.Solution("Change your code to use [ThreadStatic] or [ContextStatic] attributes.")]
public class AvoidLocalDataStoreSlotRule : Gendarme.Framework.Rule, Gendarme.Framework.IMethodRule

Remarks

This rule is available since Gendarme 2.8

Example

Bad example (Thread Local Storage):

Example

            static void SetSharedKey (byte[] key)
            {
            	LocalDataStoreSlot lds = Thread.AllocateNamedDataSlot ("shared-key");
            	lds.SetData (key.Clone ());
            }
            public byte[] SignData (byte[] data)
            {
            	LocalDataStoreSlot lds = Thread.GetNamedDataSlot ("shared-key");
            	using (HMACSHA1 hmac = new HMACSHA1 (Thread.GetData (lds) as byte[])) {
            		return hmac.ComputeHash (data);
            	}
            }
            

Example

Good example (Thread Local Storage):

Example

            [ThreadStatic]
            static byte[] shared_key;
            static void SetSharedKey (byte[] key)
            {
            	shared_key = (byte[]) key.Clone ();
            }
            public byte[] SignData (byte[] data)
            {
            	using (HMACSHA1 hmac = new HMACSHA1 (shared_key)) {
            		return hmac.ComputeHash (data);
            	}
            }
            

Requirements

Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.8.0.0