0

我想在 ioc 中使用结构映射。

这是我的代码:

public class StructureMapDependencyResolverConfigurator : IDependencyResolverConfigurator
{
    public static bool _isStarted = false;

    public void Configure()
    {
        ObjectFactory.Initialize(x =>
        {
            try
            {
                RegisterTypesFromAssembly("ImenBourse.Data", "ImenBourse.Data.AdoSql", typeof(IDaoBase), x);
                RegisterTypesFromAssembly("ImenBourse.Data", "ImenBourse.Data.AdoSql", typeof(IQueryBase), x);
                RegisterTypesFromAssembly("ImenBourse.Domain.Service", "ImenBourse.Domain.Impl", typeof(IServiceBase), x);

            }
            catch (Exception e)
            {

            }
        });

    }

    public IControllerFactory ControllerFactory { get { return new StructureMapControllerFactory(); } }
    public IContainer Container { get { return ObjectFactory.Container; } }

    private void RegisterTypesFromAssembly(string assemblyInterfaceName, string assemblyImplName, Type baseType, IInitializationExpression initializer)
    {
        Assembly intAssembly = Assembly.Load(assemblyInterfaceName);
        var interfaceList = intAssembly.GetTypes().Where(assemblyType => assemblyType.IsInterface && baseType.IsAssignableFrom(assemblyType) && baseType != assemblyType).ToList();
        if (interfaceList.Count == 0)
            interfaceList = intAssembly.GetTypes().Where(assemblyType => assemblyType.IsInterface && baseType != assemblyType).ToList();

        Assembly implAssembly = Assembly.Load(assemblyImplName);
        var implList = implAssembly.GetTypes().Where(assemblyType => assemblyType.IsClass && baseType.IsAssignableFrom(assemblyType)).ToList();

        foreach (var type in interfaceList)
        {
            foreach (var implType in implList)
            {
                if (type != implType && type.IsAssignableFrom(implType))
                {
                    initializer.ForRequestedType(type).Use(implType);
                }
            }
        }
    }

当我调用 RegisterTypesFromAssembly 函数时,此行有错误

Assembly implAssembly = Assembly.Load(assemblyImplName);

我看到了这个错误:

Could not load file or assembly 'ImenBourse.Data.AdoSql' or one of its dependencies.The system cannot find the file specified.

和 ImenBourse.Data.AdoSql 程序集是:

using System.Reflection;
using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ImenBourse.Data.AdoSql")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ImenBourse.Data.AdoSql")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

我的代码中有什么错误我有错误

4

1 回答 1

0

尝试将“.dll”添加到 RegisterTypesFromAssembly 中的程序集名称中。它似乎正在寻找一个没有扩展名的文件:

RegisterTypesFromAssembly("ImenBourse.Data", "ImenBourse.Data.AdoSql.dll", typeof(IDaoBase), x);
于 2013-11-03T13:48:30.233 回答