在UnityContainer中找不到Resolve <t>方法

时间:2017-04-26 02:58:52

标签: c# .net dependency-injection unity-container

根据MSDNconstructor(props) { super(props); this.state = { players: this.props.initialPlayers }; } 类的Resolve<T>()方法使用如下:

UnityContainer

但是,我在var controller = container.Resolve<ManagementController>(); 类定义中找不到该方法。我只能看到这一点:

UnityContainer

我使用的是错误的包装吗?

这些是我安装的软件包。

public class UnityContainer : IUnityContainer, IDisposable
{
    // Other methods
    public object Resolve(Type t, string name, params ResolverOverride[] resolverOverrides);
    public IEnumerable<object> ResolveAll(Type t, params ResolverOverride[] resolverOverrides);
    // Other methods
}

3 个答案:

答案 0 :(得分:6)

答案 1 :(得分:2)

在Visual Studio中,将光标放在Resolve<T>方法上,然后按F12,它将转到:UnityContainerExtensions

这是一种扩展方法,如果需要,代码为here

答案 2 :(得分:1)

它位于UnityContainerExtensions类中。正如@CodingYoshi提到的,​​如果你做F12或去定义,你会发现全班。

这是班级。

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

    namespace Microsoft.Practices.Unity
    {
        //
        // Summary:
        //     Extension class that adds a set of convenience overloads to the Microsoft.Practices.Unity.IUnityContainer
        //     interface.
        public static class UnityContainerExtensions
        {
            //
        // Summary:
        //     Resolve an instance of the default requested type from the container.
        //
        // Parameters:
        //   container:
        //     Container to resolve from.
        //
        //   overrides:
        //     Any overrides for the resolve call.
        //
        // Type parameters:
        //   T:
        //     System.Type of object to get from the container.
        //
        // Returns:
        //     The retrieved object.
        public static T Resolve<T>(this IUnityContainer container, params ResolverOverride[] overrides);
        //
        // Summary:
        //     Resolve an instance of the requested type with the given name from the container.
        //
        // Parameters:
        //   container:
        //     Container to resolve from.
        //
        //   name:
        //     Name of the object to retrieve.
        //
        //   overrides:
        //     Any overrides for the resolve call.
        //
        // Type parameters:
        //   T:
        //     System.Type of object to get from the container.
        //
        // Returns:
        //     The retrieved object.
        public static T Resolve<T>(this IUnityContainer container, string name, params ResolverOverride[] overrides);
        //
        // Summary:
        //     Return instances of all registered types requested.
        //
        // Parameters:
        //   container:
        //     Container to resolve from.
        //
        //   resolverOverrides:
        //     Any overrides for the resolve calls.
        //
        // Type parameters:
        //   T:
        //     The type requested.
        //
        // Returns:
        //     Set of objects of type T.
        //
        // Remarks:
        //     This method is useful if you've registered multiple types with the same System.Type
        //     but different names.
        //     Be aware that this method does NOT return an instance for the default (unnamed)
        //     registration.


public static IEnumerable<T> ResolveAll<T>(this IUnityContainer container, params ResolverOverride[] resolverOverrides);


        }
    }