31 using System.Runtime.Serialization;
32 using System.Runtime.InteropServices;
33 using System.Security.Permissions;
34 using System.Threading;
35 using System.Diagnostics;
37 namespace OpenSim.Framework
46 [SerializableAttribute]
47 [ComVisibleAttribute(
false)]
48 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization =
true, ExternalThreading =
true)]
63 public Lazy(Func<T> valueFactory)
68 public Lazy(
bool isThreadSafe)
73 public Lazy(Func<T> valueFactory,
bool isThreadSafe)
79 : this(() => Activator.CreateInstance<T>(), mode)
85 if (valueFactory == null)
86 throw new ArgumentNullException(
"valueFactory");
87 this.factory = valueFactory;
89 monitor =
new object();
94 [DebuggerBrowsable(DebuggerBrowsableState.Never)]
101 if (exception != null)
112 case LazyThreadSafetyMode.None:
114 var init_factory = factory;
115 if (init_factory == null)
116 throw exception =
new InvalidOperationException(
"The initialization function tries to access Value on this instance");
120 T v = init_factory();
122 Thread.MemoryBarrier();
132 case LazyThreadSafetyMode.PublicationOnly:
134 var init_factory = factory;
138 if (init_factory != null)
148 Thread.MemoryBarrier();
154 case LazyThreadSafetyMode.ExecutionAndPublication:
162 throw exception =
new InvalidOperationException(
"The initialization function tries to access Value on this instance");
164 var init_factory = factory;
168 T v = init_factory();
170 Thread.MemoryBarrier();
182 throw new InvalidOperationException(
"Invalid LazyThreadSafetyMode " + mode);
198 throw new InvalidOperationException(
"The initialization function tries to access Value on this instance");
200 var init_factory = factory;
204 T v = init_factory();
206 Thread.MemoryBarrier();
211 factory = init_factory;
220 public bool IsValueCreated
231 return value.ToString();
233 return "Value is not created";
Lazy(Func< T > valueFactory)
Lazy(LazyThreadSafetyMode mode)
Lazy(Func< T > valueFactory, LazyThreadSafetyMode mode)
override string ToString()
Lazy(Func< T > valueFactory, bool isThreadSafe)