How to throw a compiler error if more than one mem

2020-02-01 01:36发布

问题:

Simple question, how do you force the C# compiler to throw a compilation error.


Update: Perhaps it's better to use an Assert.Fail() instead?

I have a custom-attribute that should only be applied to ONE member of a class. Inside of my other class' static method it looks for that one member and I want it to fail (not throw an exception) if more than one member had the attribute applied to it.

public class Foo
{
    [MyCustomAttribute]
    public String FooString { get; set; }

    [MyCustomAttribute]
    public String OtherFooString { get; set; }
}


public class Bar<T>
    where T : class, new()
{
    static Bar()
    {
         //If more than one member of type Foo has MyCustomAttribute
         //applied to it compile error or Assert.Fail()?
    }
}

回答1:

You can use a diagnostic directive:

#error Oops. This is an error.

or for just a warning:

#warning This is just a warning.

You'd normally want to put these in conditional blocks, I'd expect...

EDIT: Okay, now you've updated your question, you simply can't do this at compile-time. Your suggestion of using Assert.Fail punts the problem to execution time.

I would suggest you write unit tests to detect this (iterate over all the types in the assembly, and check that the attribute has only been applied at most once per type).

EDIT: In 2016... while Code Analysis as suggested by the OP isn't actually a compiler error, now that Visual Studio uses Roslyn, it's feasible to hook into the compiler and genuinely get an error from the compiler, using a Roslyn code analyzer. However, I would still personally prefer unit tests for this, as then the code could be built and tested by anyone, regardless of whether they had the Roslyn analyzer installed. There's still no way of validating this with a "purely vanilla" C# compiler.



回答2:

Over a year after this question has been asked, I realized an alternative solution to unit tests: Code Analysis.

If no Code Analysis rule exists, you could even roll your own. Just make sure to set the rule as Error instead of Warning (or make sure to treat warnings as errors). Here's an image of that screen:

Of course, I still preferred to use a Unit Test in this particular case, but I can definitely see Code Analysis as an alternative solution, especially for things such as custom naming (i.e. All my classes should end in 'Foo' or throw an error).



回答3:

Simple Answer: Ask it to compile something syntactically invalid.



回答4:

I don't believe this can be done, because it is not possible to tell compiler to check for custom errors. Of course, you can use #error, like Jon said, but there is no way to add the condition itself. Because #error will cause error each and every time.

Only thing you can be sure about is to check at runtime and then throw exception.

Edit: Damn, getting to question after it was heavily edited.