[OOP] attributes and interface both mandatory

  • Thread starter Thread starter Steve B.
  • Start date Start date
S

Steve B.

Hi,

I wonder if it is possible in C# to create an attribute and an interface,
and to force the developpement to use both or none on his classes...

For exemple, if I have :

public MyAttribute : Attribute
{}

public interface MyInterface
{}

I'd like to allow this :

[MyAttribute]
public class class1 : MyInteface
{}

or

public class class1

But neither

[MyAttribute]
public class class1
{}

nor

public class class1 : MyInterface



Is it possible usng only the C# syntax ? I can check during run-time if it
is OK, but I'd like to avoid compilationin other ways...

Thanks,
Steve
 
AFAIK, this is not possible.

You can restrict where attributes can be applied using AttributeTargets but
your requirement cannot be achieved at compile time.

-Atul, Sky Software http://www.ssware.com
Shell MegaPack For ActiveX & .Net - Windows Explorer Like Shell UI Controls
 
Ok thanks

Atul said:
AFAIK, this is not possible.

You can restrict where attributes can be applied using AttributeTargets
but your requirement cannot be achieved at compile time.

-Atul, Sky Software http://www.ssware.com
Shell MegaPack For ActiveX & .Net - Windows Explorer Like Shell UI
Controls




Steve B. said:
Hi,

I wonder if it is possible in C# to create an attribute and an interface,
and to force the developpement to use both or none on his classes...

For exemple, if I have :

public MyAttribute : Attribute
{}

public interface MyInterface
{}

I'd like to allow this :

[MyAttribute]
public class class1 : MyInteface
{}

or

public class class1

But neither

[MyAttribute]
public class class1
{}

nor

public class class1 : MyInterface



Is it possible usng only the C# syntax ? I can check during run-time if
it is OK, but I'd like to avoid compilationin other ways...

Thanks,
Steve
 
Back
Top