Esempio n. 1
0
 /// <summary>
 /// Returns a <see cref="System.String"/> that represents this instance.
 /// </summary>
 /// <returns>
 /// A <see cref="System.String"/> that represents this instance.
 /// </returns>
 public override string ToString()
 {
     if (!IsValueCreated)
     {
         return(EnvironmentEx2.GetResourceString("Lazy_ToString_ValueNotCreated"));
     }
     return(this.Value.ToString());
 }
Esempio n. 2
0
 private static object GetObjectFromMode(LazyThreadSafetyMode mode)
 {
     if (mode == LazyThreadSafetyMode.ExecutionAndPublication)
     {
         return(new object());
     }
     if (mode == LazyThreadSafetyMode.PublicationOnly)
     {
         return(PUBLICATION_ONLY_OR_ALREADY_INITIALIZED);
     }
     if (mode != LazyThreadSafetyMode.None)
     {
         throw new ArgumentOutOfRangeException("mode", EnvironmentEx2.GetResourceString("Lazy_ctor_ModeInvalid"));
     }
     return(null);
 }
Esempio n. 3
0
        private Boxed CreateValue()
        {
            Boxed boxed = null;
            LazyThreadSafetyMode mode = Mode;

            if (m_valueFactory != null)
            {
                try
                {
                    if ((mode != LazyThreadSafetyMode.PublicationOnly) && (m_valueFactory == PUBLICATION_ONLY_OR_ALREADY_INITIALIZED))
                    {
                        throw new InvalidOperationException(EnvironmentEx2.GetResourceString("Lazy_Value_RecursiveCallsToValue"));
                    }
                    var valueFactory = m_valueFactory;
                    if (mode != LazyThreadSafetyMode.PublicationOnly)
                    {
                        m_valueFactory = PUBLICATION_ONLY_OR_ALREADY_INITIALIZED;
                    }
                    return(new Boxed(valueFactory()));
                }
                catch (Exception exception)
                {
                    if (mode != LazyThreadSafetyMode.PublicationOnly)
                    {
                        _boxed = new LazyInternalExceptionHolder(exception.PrepareForRethrow());
                    }
                    throw;
                }
            }
            try { boxed = new Boxed((T)Activator.CreateInstance(typeof(T))); }
            catch (MissingMethodException)
            {
                Exception ex = new MissingMemberException(EnvironmentEx2.GetResourceString("Lazy_CreateValue_NoParameterlessCtorForT"));
                if (mode != LazyThreadSafetyMode.PublicationOnly)
                {
                    _boxed = new LazyInternalExceptionHolder(ex);
                }
                throw ex;
            }
            return(boxed);
        }