命名空间无法识别(即使它在那里) [英] Namespace not recognized (even though it is there)

查看:103
本文介绍了命名空间无法识别(即使它在那里)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

找不到类型或命名空间名称AutoMapper"(您是否缺少 using 指令或程序集引用?)

The type or namespace name 'AutoMapper' could not be found (are you missing a using directive or an assembly reference?)

有趣的是,我的项目中已经有了该引用:

The funny thing is that I have that reference in my project already:

这是我的代码:

using System.Collections.Generic;
using DataContract;
using SelectorDAL;
using AutoMapper;

namespace SpecimenSelect
{
    public class SpecimenSelect : ISpecimenSelect
    {
        public SpecimenSelect()
        {
            SetupMaps();
        }

        private static void SetupMaps()
        {
            Mapper.CreateMap<SpecimenDetail, SpecimenDetailContract>();
        }

另一个奇怪的事情是我的解决方案中有另外两个项目,它们都使用 AutoMapper 并且引用完全相同的 AutoMapper.dll 文件.他们都工作得很好.

The other weird thing is that I have two other projects in my solution that both use AutoMapper and are referencing the exact same AutoMapper.dll file. They both work perfectly fine.

这是一个屏幕截图:

这是代码(编译良好):

and here is that code (that compiles fine):

using System.Collections.Generic;
using AutoMapper;
using DataContract;
using SelectorDAL;

namespace PatientSelect
{

    public class PatientSelect : IPatientSelect
    {
        public PatientSelect()
        {
            SetupMaps();
        }

        private void SetupMaps()
        {
            Mapper.CreateMap<Patient, PatientContract>();
            Mapper.CreateMap<OrderedTest, OrderedTestsContract>();
            Mapper.CreateMap<Gender, GenderContract>();
        }

两个引用在属性页面上似乎具有相同的数据.

Both references seem to have the same data on the properties page.

我错过了什么?

我试过了:

  1. 重新启动 Visual Studio
  2. 不使用 using 语句的引用(即 AutoMapper.Mapper.CreateMap)
  3. 清理和重建

还有其他想法吗?

推荐答案

检查以确保您的项目未设置为使用 .NET Framework 4 Client Profile.

Check to make sure that your project isn't set up to use the .NET Framework 4 Client Profile.

您可以通过右键单击您的项目(不是解决方案)来检查/更改此设置,选择属性 -> 应用程序 -> 目标框架.目标框架是该页面上的下拉列表.

You can check/change this by right-clicking your project (not the solution), select Properties -> Application -> Target framework. The target framework is a dropdown on that page.

这是 Visual Studio 中的一个问题(我什至将其称为错误).AutoMapper 需要从 .NET Framework 4 Client Profile 中排除的程序集.由于您的项目正在使用该版本的框架,因此它会中断.

This is a problem in Visual Studio (I would even go so far as to call it a bug). AutoMapper requires assemblies that are excluded from the .NET Framework 4 Client Profile. Since your project is using that version of the framework it breaks.

当您引用的项目的 .NET Framework 版本高于进行引用的项目时,类似的错误将传播到构建过程.即,一个面向 4.5 的项目引用了一个面向 4.5.1 的项目会给你同样的错误.

A similar error will propagate to the build process when the .NET Framework version for the project you are referencing is higher than the project making the reference. i.e. A project targeting 4.5 that references a project targeting 4.5.1 will give you this same error.

发生这种情况时需要有更好的错误消息,因为没有合理解释为什么它不会构建,因为错误消息告诉您要引用已明确引用的程序集.

There needs to be a better error message when this happens because there is no rational explanation as to why it would not build as the error message tells you to reference an assembly you have clearly referenced.

这篇关于命名空间无法识别(即使它在那里)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆