14 [DebuggerTypeProxy(typeof(System_LazyDebugView<>))]
15 [DebuggerDisplay(
"ThreadSafetyMode={Mode}, IsValueCreated={IsValueCreated}, IsValueFaulted={IsValueFaulted}, Value={ValueForDebugDisplay}")]
16 [__DynamicallyInvokable]
17 [HostProtection(
SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
25 internal Boxed(
T value)
31 private class LazyInternalExceptionHolder
35 internal LazyInternalExceptionHolder(
Exception ex)
41 private static readonly Func<T> ALREADY_INVOKED_SENTINEL = () =>
default(
T);
43 private object m_boxed;
46 private Func<T> m_valueFactory;
49 private object m_threadSafeObj;
51 internal T ValueForDebugDisplay
59 return ((Boxed)m_boxed).m_value;
67 if (m_threadSafeObj ==
null)
71 if (m_threadSafeObj == LazyHelpers.PUBLICATION_ONLY_SENTINEL)
79 internal bool IsValueFaulted => m_boxed is LazyInternalExceptionHolder;
84 [__DynamicallyInvokable]
87 [__DynamicallyInvokable]
92 return m_boxed is Boxed;
104 [__DynamicallyInvokable]
107 [__DynamicallyInvokable]
113 boxed = (m_boxed as Boxed);
116 return boxed.m_value;
118 LazyInternalExceptionHolder lazyInternalExceptionHolder = m_boxed as LazyInternalExceptionHolder;
119 lazyInternalExceptionHolder.m_edi.Throw();
122 return LazyInitValue();
127 [__DynamicallyInvokable]
137 [__DynamicallyInvokable]
138 public Lazy(Func<T> valueFactory)
146 [__DynamicallyInvokable]
156 [__DynamicallyInvokable]
159 m_threadSafeObj = GetObjectFromMode(mode);
168 [__DynamicallyInvokable]
169 public Lazy(Func<T> valueFactory,
bool isThreadSafe)
181 [__DynamicallyInvokable]
184 if (valueFactory ==
null)
188 m_threadSafeObj = GetObjectFromMode(mode);
189 m_valueFactory = valueFactory;
199 return LazyHelpers.PUBLICATION_ONLY_SENTINEL;
216 [__DynamicallyInvokable]
221 return Environment.GetResourceString(
"Lazy_ToString_ValueNotCreated");
223 return Value.ToString();
226 private T LazyInitValue()
232 boxed = (Boxed)(m_boxed = CreateValue());
235 boxed = CreateValue();
238 boxed = (Boxed)m_boxed;
242 m_valueFactory = ALREADY_INVOKED_SENTINEL;
248 bool lockTaken =
false;
251 if (obj != ALREADY_INVOKED_SENTINEL)
257 boxed = (Boxed)(m_boxed = CreateValue());
258 Volatile.
Write(ref m_threadSafeObj, ALREADY_INVOKED_SENTINEL);
262 boxed = (m_boxed as Boxed);
265 LazyInternalExceptionHolder lazyInternalExceptionHolder = m_boxed as LazyInternalExceptionHolder;
266 lazyInternalExceptionHolder.m_edi.Throw();
280 return boxed.m_value;
283 private Boxed CreateValue()
287 if (m_valueFactory !=
null)
293 throw new InvalidOperationException(Environment.GetResourceString(
"Lazy_Value_RecursiveCallsToValue"));
295 Func<T> valueFactory = m_valueFactory;
298 m_valueFactory = ALREADY_INVOKED_SENTINEL;
300 else if (valueFactory == ALREADY_INVOKED_SENTINEL)
304 return new Boxed(valueFactory());
310 m_boxed =
new LazyInternalExceptionHolder(ex);
317 return new Boxed((
T)Activator.CreateInstance(typeof(
T)));
319 catch (MissingMethodException)
321 Exception ex2 =
new MissingMemberException(Environment.GetResourceString(
"Lazy_CreateValue_NoParameterlessCtorForT"));
324 m_boxed =
new LazyInternalExceptionHolder(ex2);
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void Write(ref bool location, bool value)
Writes the specified value to the specified field. On systems that require it, inserts a memory barri...
override string ToString()
Creates and returns a string representation of the P:System.Lazy`1.Value property for this instance.
No initialization action.
Lazy()
Initializes a new instance of the T:System.Lazy`1 class. When lazy initialization occurs,...
T Value
Gets the lazily initialized value of the current T:System.Lazy`1 instance.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
Provides a mechanism that synchronizes access to objects.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
static ExceptionDispatchInfo Capture(Exception source)
Creates an T:System.Runtime.ExceptionServices.ExceptionDispatchInfo object that represents the specif...
Lazy(LazyThreadSafetyMode mode)
Initializes a new instance of the T:System.Lazy`1 class that uses the default constructor of T and t...
Represents an exception whose state is captured at a certain point in code.
SecurityAction
Specifies the security actions that can be performed using declarative security.
static void Enter(object obj)
Acquires an exclusive lock on the specified object.
Provides information about, and means to manipulate, the current environment and platform....
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
Lazy(Func< T > valueFactory, LazyThreadSafetyMode mode)
Initializes a new instance of the T:System.Lazy`1 class that uses the specified initialization functi...
static void Exit(object obj)
Releases an exclusive lock on the specified object.
Lazy(Func< T > valueFactory)
Initializes a new instance of the T:System.Lazy`1 class. When lazy initialization occurs,...
Contains methods for performing volatile memory operations.
Enables communication with a debugger. This class cannot be inherited.
static bool Read(ref bool location)
Reads the value of the specified field. On systems that require it, inserts a memory barrier that pre...
Locks are used to ensure that only a single thread can initialize a T:System.Lazy`1 instance in a thr...
Provides support for lazy initialization.
bool IsValueCreated
Gets a value that indicates whether a value has been created for this T:System.Lazy`1 instance.
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Specifies that the class can be serialized.
static void NotifyOfCrossThreadDependency()
Notifies a debugger that execution is about to enter a path that involves a cross-thread dependency.
DebuggerBrowsableState
Provides display instructions for the debugger.
LazyThreadSafetyMode
Specifies how a T:System.Lazy`1 instance synchronizes access among multiple threads.
Provides atomic operations for variables that are shared by multiple threads.