[OOP] attributes and interface both mandatory

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
 
A

Atul

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
 
S

Steve B.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top