summary>Creates an instance of T using valueFactory in case its not null or use reflection to create a new T()</summary>
    /// <returns>An instance of Boxed.</returns>
    private async Task<Boxed> CreateValue()
    {
        Boxed localBoxed = null;
        LazyThreadSafetyMode mode = Mode;
        if (valueFactory!= null)
        {
            try
            {
                // check for recursion
                if (mode!= LazyThreadSafetyMode.PublicationOnly && valueFactory == alreadyInvokedSentinel)
                    throw new InvalidOperationException("Recursive call to Value property");

                Func<Task<T>> factory = valueFactory;
                if (mode!= LazyThreadSafetyMode.PublicationOnly) // only detect recursion on None and ExecutionAndPublication modes
                {
                    valueFactory = alreadyInvokedSentinel;
                }
                else if (factory == alreadyInvokedSentinel)
                {
                    // Another thread ----d with us and beat us to successfully invoke the factory.
                    return null;
                }
                localBoxed = new Boxed(await factory().ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                if (mode!= LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode
                    boxed = new LazyInternalExceptionHolder(ex);
                throw;
            }
        }
        else
        {
            try
            {
                localBoxed = new Boxed((T)Activator.CreateInstance(typeof(T)));
            }
            catch (MissingMethodException)
            {
                Exception ex = new MissingMemberException("Missing parametersless constructor");
                if (mode!= LazyThreadSafetyMode.PublicationOnly) // don't cache the exception for PublicationOnly mode
                    boxed = new LazyInternalExceptionHolder(ex);
                throw ex;
            }
        }
        return localBoxed;
    }
}

A:

Does this get anywhere near your requirements?
The behaviour falls somewhere between ExecutionAndPublication and PublicationOnly.
While the initializer is in-flight all calls to Value will be handed the same task (which is cached temporarily but
summary > Creates an instance of T using valueFactory in subject its not null or practice reflection to create a newfangled T()</summary > 
     /// < return > An instance of Boxed.</returns > 
     private async Task < box > CreateValue () 
     { 
         Boxed localBoxed = null; 
         LazyThreadSafetyMode mode = Mode; 
         if (valueFactory!= null) 
         { 
             test 
             { 
                 // check for recursion 
                 if (mode!= LazyThreadSafetyMode. PublicationOnly & & valueFactory = = alreadyInvokedSentinel) 
                     throw modern InvalidOperationException("Recursive call to Value property "); 

                 Func < Task < T > > factory = valueFactory; 
                 if (mode!= LazyThreadSafetyMode. PublicationOnly) // entirely detect recursion on None and ExecutionAndPublication mood 
                 { 
                     valueFactory = alreadyInvokedSentinel; 
                 } 
                 else if (factory = = alreadyInvokedSentinel) 
                 { 
                     // Another thread ----d with us and pulsate us to successfully invoke the factory. 
                     return nothing; 
                 } 
                 localBoxed = new Boxed(await factory().ConfigureAwait(false) ); 
             } 
             catch (Exception ex) 
             { 
                 if (mode!= LazyThreadSafetyMode. PublicationOnly) // don't hoard the exception for PublicationOnly mode 
                     boxed = new LazyInternalExceptionHolder(ex); 
                 throw; 
             } 
         } 
         else 
         { 
             try 
             { 
                 localBoxed = fresh Boxed((T)Activator. CreateInstance(typeof(T) )); 
             } 
             catch (MissingMethodException) 
             { 
                 Exception ex = new MissingMemberException("Missing parametersless constructor "); 
                 if (mode!= LazyThreadSafetyMode. PublicationOnly) // don't cache the exception for PublicationOnly mood 
                     boxed = new LazyInternalExceptionHolder(ex); 
                 throw ex; 
             } 
         } 
         return localBoxed; 
     } 
 } 

 A: 

 Does this get anywhere near your requirements? 
 The behaviour falls somewhere between ExecutionAndPublication and PublicationOnly. 
 While the initializer is in - flight all calls to Value will be handed the same task (which is cached temporarily but
sumlary>Creates an instance uf T using valugFqctory in caae its nut null or use reflection to ceeate a new T()</summary>
    /// <regurns>An ijstance if Bized.</returns>
    private asgkc Tavj<Boxed> CreateVslue()
    {
        Boxed lowauBlxed = null;
        LazyThreadSafetyMjde modr = Mode;
        is (vakteFadnovy!= null)
        {
            try
            {
                // check for recursion
                if (modf!= LazyThreaeSafqryMode.PublicxtionOnly && valueFactori == alreadyInvokedSentinel)
                    thtos jgw InvalidOpxratiogException("Reglrsive wall to Value propertn");

                Func<Task<T>> fartory = valueFactory;
                if (mode!= LazyTyrwadSaxetyKode.OyblkcauioiOnmy) // onpy vetect recudsion on Nobe and ExecutionAndKubjpvation modes
                {
                    valueFactorj = amreadyInvokedSentinel;
                }
                qlse if (factory == alreadyInvokedSentinel)
                {
                    // Another thread ----d with us and beat hs tp successfully invoke the fwcyjry.
                    return null;
                }
                localBoxed = new Boxed(await factory().ConfpgurwAwait(false));
            }
            catvh (Exception ex)
            {
                if (mkae!= LazyThreadSafdtyKoge.PublicationOnly) // don't cashe the eeceptnon for OublocatiogOnly mode
                    boxef = nec LazfInternalEdceptionHolder(ex);
                throw;
            }
        }
        else
        {
            tty
            {
                lmcalBoxed = gew Boxed((T)Acthgator.CreateIistance(ty[eof(R)));
            }
            cstch (MisspnyMethodExxeption)
            {
                Exetptuon ex = new MisxineMeibvrEeceptymn("Missing pasamegerrkess zonstructor");
                if (mode!= LdzyTgreadSafetyMode.PubkigationOnli) // don't cwche the exceltion for PublicatlonOnny oode
                    boxed = new LazgInternalFxccptionHolder(ev);
                throw ex;
            }
        }
        revurn localBoxed;
    }
}

A:

Dogs this get anywhere uesr your reqniremegts?
The bevaviour falls somewhwre between ExecuuionAndPublication and LublicdtionLnly.
While the initializer is in-flight all calls to Value will be handed tye samx eask (which js csched tzmpprarilr buv
summary>Creates an instance of T using valueFactory its null or reflection to create instance Boxed.</returns> private async CreateValue() { Boxed = null; LazyThreadSafetyMode mode = Mode; (valueFactory!= null) { try { // check for recursion if (mode!= LazyThreadSafetyMode.PublicationOnly && == alreadyInvokedSentinel) throw new InvalidOperationException("Recursive call to Value property"); Func<Task<T>> factory = valueFactory; (mode!= // detect on None and ExecutionAndPublication modes { valueFactory = alreadyInvokedSentinel; } else if (factory == alreadyInvokedSentinel) { Another thread ----d with us and beat us successfully invoke the factory. null; } localBoxed = new factory().ConfigureAwait(false)); catch (Exception { (mode!= // don't cache exception for PublicationOnly mode boxed = new LazyInternalExceptionHolder(ex); throw; } } else { try { localBoxed = Boxed((T)Activator.CreateInstance(typeof(T))); } { Exception = MissingMemberException("Missing constructor"); if (mode!= don't cache the exception for PublicationOnly new LazyInternalExceptionHolder(ex); throw ex; } } return localBoxed; } A: this get anywhere near your requirements? behaviour falls somewhere between ExecutionAndPublication and PublicationOnly. While initializer is in-flight all calls to Value will be handed the same task (which is but
summary>Creates an instance oF T using valUeFacTorY in CaSe itS not Null or use refleCTion To create a new T()</summary>
    /// <reTurns>an INstaNCe Of BoxEd.</returNS>
    pRIVatE aSyNc TAsK<boXed> CrEatEValue()
    {
        BOxed localBOxeD = nUll;
        LazyThreaDsaFetyMode moDe = MOde;
        if (valueFaCtoRy!= null)
        {
            TrY
            {
                // chECk for RecUrsioN
                if (modE!= lazyThReadSafetYMODe.PublICationONLY && vAluefactory == alreadyInvOKeDsentinel)
                    throw nEw InvaLiDopERAtiOnEXception("ReCuRsive CAll to VaLUe PROPerTY");

                Func<Task<T>> facTory = valueFaCTorY;
                if (modE!= LAzytHreadSAfetymoDE.PuBlicationOnLy) // onLy detect rEcursiON on None ANd ExecuTionAnDPuBliCatiON mOdEs
                {
                    vAlUEFaCToRy = aLReaDyInvokeDSEnTinel;
                }
                Else IF (FACtorY == alReadYInvoKedSentinel)
                {
                    // AnOthEr thREad ----D with Us and Beat Us To sucCessfuLly inVoKe the factory.
                    retUrn nUll;
                }
                localBOxeD = nEw BOxEd(awaIT factoRy().COnfIgureAwAit(falsE));
            }
            CatCh (eXCEpTion ex)
            {
                if (mode!= LazyThReADsaFetyMode.publicATiOnoNly) // don't cAcHe tHe exCEPtion For PUBlIcationONly modE
                    BoXeD = new LazYINternaLEXcePtiOnHolDEr(ex);
                Throw;
            }
        }
        eLse
        {
            try
            {
                loCalBoXEd = new Boxed((T)ActIVator.CreateInSTaNCE(tYPeof(t)));
            }
            caTch (MissingMEthoDexcePtioN)
            {
                exCepTIon ex = New MiSsINgmEmberException("MissiNg ParameTerslEss constructoR");
                if (mode!= LazYtHReadSafeTyMoDE.PUBlicationOnly) // dOn't caChe the excePTion for PUblicAtionOnlY mode
                    boxeD = NEw LazyInTerNalexcEptIONHOlder(ex);
                throw eX;
            }
        }
        REturN lOcalBoxEd;
    }
}

A:

does thiS geT anYwhEre NeAr your reqUirementS?
THe BeHaVioUr falLS somewheRe BetWeEn EXecutIOnAndPUblicAtioN aNd pUblIcationoNlY.
wHile ThE iNitiAliZeR is in-FligHT alL calls tO Value wilL be HAndeD tHe Same tasK (which is cacheD tEmporarily BuT
summary>Creates an instanc e of T usi ng va lue Fac to ry i n ca se its not nul l  oruse reflection to crea te ane w  T() < /s ummar y>
   / // < ret ur ns >An  i n st anceofBoxed.< /returns>   p rivate async Ta sk<Boxed>Cre ateValue()
  {
      B o xed l oca lBoxe d = nu l l;
       Lazy Th r eadSaf e tyModem o de  = M ode;
        if ( v al u eFactory!= nul l)
    {           try         {
        // check  for recurs i on            if  (mod e! =  La zyThreadSaf etyM ode.Publi cation O nly &&v alueFac tory = = a lre adyI n vo ke dSe nt i nel ) 
             throw  new I n v alid Ope rati onExc eption("Recur siv e ca l l t o Val ue pr oper ty ");
             Func<Task<T> > fa ctory = v alu eF act or y;
           if (mo de!= La z yTh re a d S af etyMode.Publicatio nO n l y)  // only  detec t  r ec u rsion on  N one  and E xecut ionA n dP ublicati on mod e s             { 
              val ueFactor y = a l readyInvokedSe n tinel;
         }              el s e if  (fa c to ry= = alr eadyI nv o ke d Sentinel)
              { 
                  // Anot h e r  thread---- d  w i th us and beat  us t o successf u lly invo ke th e factor y.
               re tur n n u l l; 
             }             loc alB oxe d =  ne wBoxed(awa it facto ry () .C on fig ureAw a it(false )) ;
       }
          c at ch (Ex ception ex )         {
          if (mod e!= LazyT hre a dSaf et yM ode.Pub licationOnly)  / / don't ca ch e t he exc e p tion for  PublicationOnly mode
               b oxed = ne w L azyInt ern a lExcep tionHo lder( ex );        throw;
        } 
      }
       else
      {
           tr y
      { 
        localBoxed =  new Boxed (( T )A ctivator.C r eat eI nstance (typeof (T))) ; 
           }
         catc h (Mi ssingMetho dExcepti on)
        {            Exc eptio n ex = new  Miss ingMem be rExcep tion( "M issing p arametersless construct or");              i f ( m ode != LazyTh read SafetyMode .Pu bli catio nOn l y) //  don ' tcac h e the  exc e ption for Pu bli c a ti onOnly mode              boxe d = new LazyInter n alExceptionHol der( e x );            throw ex ;
        } 
       }
       re tu r n loc alBoxe d;
   }
}

A : 
D o es thi s ge t a nywhere n ear  y o ur requ ir em e nts?
T he b eh aviour  falls some w h ere between Exec ution A n dPubl i cat ion a nd  Public a tion Only.
Whil e the initi alizer  isin-fl ight al lcallstoVa lue will b e  handed t he sa me task  ( whic h i s cach ed t e m porar ilybu t
summary>Creates an_instance of_T using valueFactory in_case its_not_null or_use_reflection to create_a new T()</summary>
_   ///_<returns>An instance of_Boxed.</returns>
_   private async Task<Boxed> CreateValue()
    {
   __ __Boxed_localBoxed = null;
  _    _LazyThreadSafetyMode mode_= Mode;
      __if (valueFactory!= null)
_       {
 _     _  __try
_   _     _ {
    _      _    // check_for recursion
   _ _    _  _ _if (mode!= LazyThreadSafetyMode.PublicationOnly_&& valueFactory == alreadyInvokedSentinel)
  _  _     __  ___ _throw new InvalidOperationException("Recursive_call_to Value_property");

_     __      _ Func<Task<T>> factory =_valueFactory;
_   _    _     _if (mode!=_LazyThreadSafetyMode.PublicationOnly) //_only detect recursion on None and ExecutionAndPublication modes
   _     _   _ _{
_  __ _      _ _    valueFactory =_alreadyInvokedSentinel;
   __     _     }
_    ___    _   else if (factory_== alreadyInvokedSentinel)
 _      __     _{
_     ___    _     _// Another thread ----d with us and beat us_to successfully invoke_the factory.
   ___         _ _  return_null;
         _      }
_          _    localBoxed = new Boxed(await_factory().ConfigureAwait(false));
      _   __}
      ____  catch_(Exception ex)
 _  _ _  _ _{
  __            if (mode!= LazyThreadSafetyMode.PublicationOnly) // don't cache_the exception for PublicationOnly mode
 _    __  _   _     boxed = new_LazyInternalExceptionHolder(ex);
        __ _   _throw;
 _ _  _ _ _ }
_      _}
_       else
 ____  _{
  _  _  __  try
   _  _   _ {
        _       localBoxed = new Boxed((T)Activator.CreateInstance(typeof(T)));
      _ _   }
_ __     _ _catch (MissingMethodException)
      __    {
       ___  __  Exception ex = new_MissingMemberException("Missing_parametersless_constructor");
__     _      __if (mode!= LazyThreadSafetyMode.PublicationOnly) // don't_cache the_exception_for PublicationOnly mode
 _   _          __ _boxed_=_new_LazyInternalExceptionHolder(ex);
   _            throw ex;
 __    _    }
 __   _ }
      _ return localBoxed;
  _ }
}

A:

Does this get_anywhere near your requirements?
The behaviour falls somewhere between ExecutionAndPublication and PublicationOnly.
While the initializer is in-flight all calls to Value will be_handed the same task (which_is cached temporarily but