Is there TryResolve in Unity?

前端 未结 6 537
独厮守ぢ
独厮守ぢ 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 02:02

    This has been an issue on the codeplex site, you can find the code here (look at the bottom of that thread and they have made an extension method...very handy)

    http://unity.codeplex.com/Thread/View.aspx?ThreadId=24543

    and the you can use code like this:

    if (container.CanResolve() == true)
    {
        try
        {
            return container.Resolve();
        }
        catch (Exception e)
        {
            // do something else
        }
    }
    

    CanResolve is that extension method. I'm actually registering that extension upon creation of the container...something like this:

    private void CreateContainer()
    {
        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
    
        map.ExeConfigFilename = // path to config file
    
        // get section from config code goes here
    
        IUnityContainer container = new UnityContainer();
        container.AddNewExtension();
        section.Containers.Default.Configure(container);        
    }
    

提交回复
热议问题