简体   繁体   English

未找到自定义 C# 属性的命名空间

[英]Namespace not found for Custom C# attribute

We have a Model that is passed from a web page as json and uses model building in our controller to resolve the properties within our .Net Core 3.1 server.我们有一个从网页作为 json 传递的模型,并在我们的控制器中使用模型构建来解析 .Net Core 3.1 服务器中的属性。 We want to decorate some properties of that model with a custom attribute so that a "helper" method can perform a transformation on the specified property in our Controller.我们想用自定义属性装饰该模型的一些属性,以便“帮助器”方法可以对控制器中的指定属性执行转换。

When we have NO attributes on our model, everything compiles and works fine.当我们的模型上没有属性时,一切都可以编译并正常工作。

We have created a class for this model (LoginModel) and decorated a property with our custom attribute [Courier].我们为此模型创建了一个类 (LoginModel),并使用我们的自定义属性 [Courier] 修饰了一个属性。

namespace THO.Demo.API.Models
{

    public class LoginModel
    {
        public string Email { get; set; }
        [Courier(foo: "secret")]
        public string Secret { get; set; }
        [Courier]
        public string PIN { get; set; }
    }
}

We have created a class for the Custom Attribute我们为自定义属性创建了一个类

namespace THO.Demo.API.Models
{
    [AttributeUsage(AttributeTargets.Property)]
    public class CourierAttribute : System.Attribute
    {
        private string foobar;
        public CourierAttribute(string foo)
        {
            foobar = foo;
        }

        public CourierAttribute() { }
    }
}

When I try to build our project, I get the following errors:当我尝试构建我们的项目时,出现以下错误:

Error CS0246 The type or namespace name 'CourierAttribute' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246 找不到类型或命名空间名称“CourierAttribute”(您是否缺少 using 指令或程序集引用?)

and

Error CS0246 The type or namespace name 'Courier' could not be found (are you missing a using directive or an assembly reference?)错误 CS0246 找不到类型或命名空间名称“Courier”(您是否缺少 using 指令或程序集引用?)

Both of these files are "side by side" in the same project and in the same namespace.这两个文件在同一个项目和同一个命名空间中“并排”。 I have tried naming the Attribute "MyCustomAttribute" with the same issue.我曾尝试将具有相同问题的属性命名为“MyCustomAttribute”。 When (In VS2019) I click on the attribute, I see a reference and can "jump" to it, so it appears that at least VS knows they are there, but the project will not compile.当(在 VS2019 中)我点击属性时,我看到一个引用并且可以“跳转”到它,所以看起来至少 VS 知道它们在那里,但项目不会编译。

Any thoughts?有什么想法吗? Thanks.谢谢。

Our way of organizing projects is to keep all of our models ({projectRoot}/Models) in a folder and namespace.我们组织项目的方式是将我们所有的模型 ({projectRoot}/Models) 保存在一个文件夹和命名空间中。 We also created a folder for "Attributes" ({projectRoot}/Attributes) where we put our attribute class.我们还为“属性”({projectRoot}/Attributes)创建了一个文件夹,我们将属性类放在其中。 When I moved the "LoginModel" from our Models folder into the root folder of the project, the compiler was able to find the attribute (even when we leave the Attribute class in its own folder).当我将“LoginModel”从我们的 Models 文件夹移动到项目的根文件夹时,编译器能够找到该属性(即使我们将 Attribute 类保留在其自己的文件夹中)。 This makes no sense, as all we did was move the file.这毫无意义,因为我们所做的只是移动文件。 We did not change the namespace.我们没有更改命名空间。 This seems to be a work around, but it does break our project organization.这似乎是一种解决方法,但它确实破坏了我们的项目组织。 (BTW, when we do NOT have a property decorated with an attribute, the model is found and compiles correctly). (顺便说一句,当我们没有用属性修饰的属性时,模型会被找到并正确编译)。 Update - the "not found" issue was in a separate project (oops) with a link to the LoginModel.更新 - “未找到”问题位于一个单独的项目 (oops) 中,并带有指向 LoginModel 的链接。 Once we looked carefully at the eaxact error message and added a link to the Attribute class, everything works.一旦我们仔细查看 eaxact 错误消息并添加到 Attribute 类的链接,一切正常。

Lesson learned - CAREFULLY examine the exact error message.经验教训 - 仔细检查确切的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM