So werfen Sie einen Compiler -Fehler, wenn mehr als ein Mitglied das gleiche Attribut hat

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: So werfen Sie einen Compiler -Fehler, wenn mehr als ein Mitglied das gleiche Attribut hat

by Anonymous » 05 Mar 2025, 12:00

Einfache Frage: Wie erzwingen Sie den C# Compiler, einen Kompilierungsfehler zu werfen. In der statischen Methode meiner anderen Klasse sucht es für dieses eine Mitglied und ich möchte, dass es fehlschlägt (keine Ausnahme ausgelöst), wenn mehr als ein Mitglied das Attribut darauf angewendet hat. < /P>

Code: Select all

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

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

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

Top