473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Attributes and interfaces

I have an interface which has a bunch of methods defined that all the
inheriting classes will have to implement.

How can I force the inheriting classes to have a class attribute?
Something like this (except it does not compile)

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class TestAttribute : Attribute
{
public TestAttribute (string name)
{
Console.Writeline(name);
}
}

[TestAttribute()]
public interface IReport
{
public void Execute();
}

[TestAttribute("Invoice Report")]
public class CoolReport
{
public void Execute()
{
Report.Run();
}
}
Jun 14 '07 #1
3 2227
Frank,

You can place attributes on interfaces, but you can't enforce that
implementing classes declare an attribute on them. You have to make this a
runtime check, and then fail your operation (I assume) if it doesn't exist
on classes that implement the interface.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Frank Rizzo" <no**@none.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I have an interface which has a bunch of methods defined that all the
inheriting classes will have to implement.

How can I force the inheriting classes to have a class attribute?
Something like this (except it does not compile)

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class TestAttribute : Attribute
{
public TestAttribute (string name)
{
Console.Writeline(name);
}
}

[TestAttribute()]
public interface IReport
{
public void Execute();
}

[TestAttribute("Invoice Report")]
public class CoolReport
{
public void Execute()
{
Report.Run();
}
}
Jun 15 '07 #2

Can't, use a property in the interface instead (ReportName in this
case).

will be easier to program against and get data out anyways..

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Thu, 14 Jun 2007 16:10:48 -0700, Frank Rizzo <no**@none.comwrote:
>I have an interface which has a bunch of methods defined that all the
inheriting classes will have to implement.

How can I force the inheriting classes to have a class attribute?
Something like this (except it does not compile)

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class TestAttribute : Attribute
{
public TestAttribute (string name)
{
Console.Writeline(name);
}
}

[TestAttribute()]
public interface IReport
{
public void Execute();
}

[TestAttribute("Invoice Report")]
public class CoolReport
{
public void Execute()
{
Report.Run();
}
}
Jun 15 '07 #3
>I have an interface which has a bunch of methods defined that all the
>inheriting classes will have to implement.

How can I force the inheriting classes to have a class attribute?
This is off the top of my head without having tried it.

Could you make a class with abstract virtual methods that do the same thing?

public class Stuff {

[SomeAttribute]
public abstract void DoStuff(int toThis);
[SomeAttribute]
public abstract int GetStuff(string fromHere);
}

Or, as many of us are guilty of, you could make a template for your
interface implementation. Not a "Template Design Pattern", I mean a
template that is buried deep within C:\Program Files\Microsoft Visual Studio
2003 folder tree, so that you can add a new class to your project, and the
Add New Class dialog will display an item titled "IDoStuff Implementation".
Your template can have the class declaration, method stubs, attributes and
all.

I can never remember where VS stores its project item templates, but you
could easily do a search in "C:\Program Files\Visual Studio 2003" for *.cs
Containing Text "public class [!".

I have made a template for classes that use COM+ Enterprise Services, and it
sprinkles all the attributes in all the right places, as well as lots of
inherited overrides. All I have to do is replace the GUIDs and modify the
items I don't like for a given implementation.

It's not pretty, but it works.

--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"The power to query ADSI is a power only
one has achieved, but if we work together,
I know we can discover the secrets."
-- The Emperor
Jun 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
32
by: Kalle Anke | last post by:
I'm coming to Python from other programming languages. I like to hide all attributes of a class and to only provide access to them via methods. Some of these languages allows me to write something...
5
by: Doug Holland | last post by:
Often you see code where an empty interface is used to indicate something about the class that realizes it. In the .NET world this can be done with custom attributes too, so which is better: ...
0
by: Jussi | last post by:
Hi! I have a COM server object in which I have set certain interfaces restricted/hidden. For example With VB client these attributes works as excpected. However if I use this same COM...
5
by: Nuno Magalhaes | last post by:
How can I get all the attributes of an HttpWebResponse? I do a request to this site: http://pipa.inov.pt/cgi-bin/camera but originaly it comes with a PAGE attribute that gives me the time in clock...
1
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm a little bit consfused as to when to use each of these options. For example, if i want to decorate a class with some information, I could decalre a const or read only field and have...
1
by: Steve K. | last post by:
I added a property to an interface like this: <code> public interface ISimpleAddress { string CarrierFax{ get; set; } } </code>
1
markmcgookin
by: markmcgookin | last post by:
Hi Folks, I am having a problem here, I use a piece of software to create xml based interfaces for an application, and I can bind these interfaces datasource to a dataset that I am currently...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.