Start Debugging
2023-09-14 Aktualisiert 2023-11-05 csharpdotnetdotnet-8 Edit on GitHub

The type or namespace name InterceptsLocationAttribute could not be found

So beheben Sie den Fehler CS0246 für InterceptsLocationAttribute bei C#-Interceptors, indem Sie das Attribut selbst definieren.

Wer mit Interceptors gerade erst beginnt, sieht eventuell einen der folgenden Fehler:

Error CS0246 The type or namespace name ‘InterceptsLocationAttribute’ could not be found (are you missing a using directive or an assembly reference?)

Error CS0246 The type or namespace name ‘InterceptsLocation’ could not be found (are you missing a using directive or an assembly reference?)

Der Grund: Das Attribut ist noch nirgendwo definiert, deshalb müssen Sie es selbst anlegen. Keine Sorge, der Compiler erkennt Ihr Attribut korrekt und wendet das erwartete Verhalten an.

Hier eine Definition für das InterceptsLocation-Attribut, die Sie verwenden können:

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    sealed class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
    {
    }
}

Error CS8652 The feature ‘primary constructors’ is currently in Preview and unsupported. To use Preview features, use the ‘preview’ language version.

Das bedeutet, dass Sie zwar .NET 8 verwenden, aber noch nicht auf C# 12 umgestellt haben. Sie können entweder auf C# 12 umstellen oder das Attribut ohne Primary Constructors definieren, etwa so:

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    sealed class InterceptsLocationAttribute : Attribute
    {
        public InterceptsLocationAttribute(string filePath, int line, int character)
        {
            
        }
    }
}

Comments

Sign in with GitHub to comment. Reactions and replies thread back to the comments repo.

< Zurück