5 // Zoltan Varga (vargaz@gmail.com)
6 // Marek Safar (marek.safar@gmail.com)
8 // Copyright (C) 2009 Novell
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Runtime.Serialization;
34 using System.Runtime.InteropServices;
35 using System.Security.Permissions;
36 using System.Threading;
37 using System.Diagnostics;
41 [SerializableAttribute]
42 [ComVisibleAttribute(false)]
43 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
48 LazyThreadSafetyMode mode;
54 : this (LazyThreadSafetyMode.ExecutionAndPublication)
58 public Lazy (Func<T> valueFactory)
59 : this (valueFactory, LazyThreadSafetyMode.ExecutionAndPublication)
63 public Lazy (bool isThreadSafe)
64 : this (() => Activator.CreateInstance<T> (), isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
68 public Lazy (Func<T> valueFactory, bool isThreadSafe)
69 : this (valueFactory, isThreadSafe ? LazyThreadSafetyMode.ExecutionAndPublication : LazyThreadSafetyMode.None)
73 public Lazy (LazyThreadSafetyMode mode)
74 : this (() => Activator.CreateInstance<T> (), mode)
80 public Lazy (Func<T> valueFactory, LazyThreadSafetyMode mode)
82 if (valueFactory == null)
83 throw new ArgumentNullException ("valueFactory");
84 this.factory = valueFactory;
85 if (mode != LazyThreadSafetyMode.None)
86 monitor = new object ();
90 // Don't trigger expensive initialization
91 [DebuggerBrowsable (DebuggerBrowsableState.Never)]
96 if (exception != null)
105 case LazyThreadSafetyMode.None: {
106 var init_factory = factory;
107 if (init_factory == null)
108 throw exception = new InvalidOperationException ("The initialization function tries to access Value on this instance");
111 T v = init_factory ();
113 Thread.MemoryBarrier ();
115 } catch (Exception ex) {
121 case LazyThreadSafetyMode.PublicationOnly: {
122 var init_factory = factory;
125 //exceptions are ignored
126 if (init_factory != null)
135 Thread.MemoryBarrier ();
141 case LazyThreadSafetyMode.ExecutionAndPublication: {
147 throw exception = new InvalidOperationException ("The initialization function tries to access Value on this instance");
149 var init_factory = factory;
152 T v = init_factory ();
154 Thread.MemoryBarrier ();
156 } catch (Exception ex) {
164 throw new InvalidOperationException ("Invalid LazyThreadSafetyMode " + mode);
167 if (monitor == null) {
176 throw new InvalidOperationException ("The initialization function tries to access Value on this instance");
178 var init_factory = factory;
181 T v = init_factory ();
183 Thread.MemoryBarrier ();
186 factory = init_factory;
195 public bool IsValueCreated {
201 public override string ToString ()
204 return value.ToString ();
206 return "Value is not created";