Is there TryResolve in Unity?

前端 未结 6 545
独厮守ぢ
独厮守ぢ 2020-12-11 01:42

How can I make Unity not to throw ResolutionFailedException if Resolve fails?

Is there something like TryResolve<

6条回答
  •  不知归路
    2020-12-11 01:48

    This is not available in the current release. However, you can always "roll your own" using extension methods in C# 3. Once Unity supports this, you can omit or update the extension method.

    public static class UnityExtensions
    {
        public static T TryResolve( this UnityContainer container )
            where T : class
        {
            try
            {
                return (T)container.Resolve( typeof( T ) );
            }
            catch( Exception )
            {
                return null;
            }
        }
    }
    

提交回复
热议问题