C#4 编译器中的奇怪错误?别名命名空间变得混乱

发布于 2024-09-29 22:04:07 字数 2602 浏览 0 评论 0原文

这是我的问题,

我使用命名空间来消除工厂类的歧义,该工厂类从实体框架实体对象(PO​​CO 工厂,..如果有意义的话!)创建域对象。你可以说我很老式,但我喜欢这种方式 :-)

我正在使用的命名空间就是这样的别名 -

using Entities = DataAccess.Models.AccessControl;
using Cases = DomainModel.BusinessObjects.Implimentations.Cases;

现在,DomainModel.BusinessObjects.Implementations.Cases 命名空间到目前为止只有一种类型,称为案例类型。然而,当我正在研究另一种使用 CaseType 类的类型时,我注意到当我“点”到 Cases 别名时,它指向我的 CaseType 中一个完全不同的命名空间。 code>DataAccess 程序集,并在智能感知中为我提供了 CaseTypeFactory

alt text

所以我检查了 CaseTypeCaseTypeFactory 类,它们命名空间正确。天哪,这是怎么回事?这件事我实在是没法解决。

这是 CaseTypeCaseTypeFactory 的代码(如果有帮助的话)。

CaseTypeFactory

using Domain = DomainModel.BusinessObjects.Implimentations.Cases;
using Entities = DataAccess.Models.AccessControl;
using Interfaces.DataAccess;

    namespace DataAccess.Factories.Cases
    {
        public class CaseTypeFactory :
            IEntityPOCOFactory<Domain.CaseType, Entities.CaseType>
        {
            #region IEntityPOCOFactory<CaseType,CaseType> Members

            public Domain.CaseType CreatePOCO(Entities.CaseType entity)
            {
                return new Domain.CaseType(entity.Id, entity.Name, entity.LastChanged);
            }

            public IList<Domain.CaseType> CreatePOCOs(
                IEnumerable<Entities.CaseType> entities)
            {
                var toReturn = new List<Domain.CaseType>();

                foreach (var entity in entities)
                {
                    toReturn.Add(CreatePOCO(entity));
                }
                return toReturn;
            }

            #endregion
        }
    }

CaseType

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.BusinessObjects.Base;

namespace DomainModel.BusinessObjects.Implimentations.Cases
{
    public class CaseType : NamedNumberedAndVersioned
    {
        public CaseType(string name, DateTime lastChanged)
            : this(0, name, lastChanged) { }

        public CaseType(int id, string name, DateTime lastChanged)
            : base(id, name, lastChanged) { }

        public void Update(string name)
        {
            this.Name = name;
        }
    }
}

可能值得一提的是,我正在使用 .Net 4.0 / VS2010

任何帮助将不胜感激。 提前致谢。

Here's my issue,

I'm using a namespace to remove ambiguity from a factory class which is creating domain objects from entity framework entity objects (a POCO factory,.. if that makes sense!). Call me old fashioned but I like things this way :-)

The namespaces I'm working with are aliased as such -

using Entities = DataAccess.Models.AccessControl;
using Cases = DomainModel.BusinessObjects.Implimentations.Cases;

Now, the DomainModel.BusinessObjects.Implimentations.Cases namespace only has one type so far called CaseType. However whilst I was working on another type which consumes the CaseType class I noticed that when I 'dot' into the Cases alias, it points to a totally different namespace in my DataAccess assembly and gives me the CaseTypeFactory in intellisense.

alt text

So I checked the CaseType and CaseTypeFactory classes and they are namespaced correctly. What in god's name is going on? I really can't work this one out.

Here's the code for the CaseType and CaseTypeFactory if it helps.

CaseTypeFactory

using Domain = DomainModel.BusinessObjects.Implimentations.Cases;
using Entities = DataAccess.Models.AccessControl;
using Interfaces.DataAccess;

    namespace DataAccess.Factories.Cases
    {
        public class CaseTypeFactory :
            IEntityPOCOFactory<Domain.CaseType, Entities.CaseType>
        {
            #region IEntityPOCOFactory<CaseType,CaseType> Members

            public Domain.CaseType CreatePOCO(Entities.CaseType entity)
            {
                return new Domain.CaseType(entity.Id, entity.Name, entity.LastChanged);
            }

            public IList<Domain.CaseType> CreatePOCOs(
                IEnumerable<Entities.CaseType> entities)
            {
                var toReturn = new List<Domain.CaseType>();

                foreach (var entity in entities)
                {
                    toReturn.Add(CreatePOCO(entity));
                }
                return toReturn;
            }

            #endregion
        }
    }

CaseType

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.BusinessObjects.Base;

namespace DomainModel.BusinessObjects.Implimentations.Cases
{
    public class CaseType : NamedNumberedAndVersioned
    {
        public CaseType(string name, DateTime lastChanged)
            : this(0, name, lastChanged) { }

        public CaseType(int id, string name, DateTime lastChanged)
            : base(id, name, lastChanged) { }

        public void Update(string name)
        {
            this.Name = name;
        }
    }
}

It's probably worth mentioning I'm working with .Net 4.0 / VS2010

Any help would be massively appreciated.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜扑 2024-10-06 22:04:07

您正在 DataAccess.Factories 命名空间中编写的代码吗?如果是这样,那么 Cases 确实会首先解析为 DataAccess.Factories.Cases

两个选项:

  • 使用一个不是另一个命名空间的名称的别名,例如

    使用 DomainCases = ...
    
  • 使用::而不是。< /code> 强制它使用别名:

    IEntityPOCOFactory<案例::CaseType,无论什么>
    

我个人会选择第一个选项以使事情更清晰。 ..或者尝试避免要求名称空间别名开头。

Is the code you're writing in the DataAccess.Factories namespace? If so, then Cases will indeed resolve to DataAccess.Factories.Cases first.

Two options:

  • Use an alias which isn't also the name of another namespace, e.g.

    using DomainCases = ...
    
  • Use :: instead of . to force it to use the alias:

    IEntityPOCOFactory<Cases::CaseType, Whatever>
    

I'd personally go for the first option to make things clearer... or try to avoid requiring namespace aliases to start with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文