Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Attributes and interfaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Frank Rizzo - 15 Jun 2007 00:10 GMT
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();
    }
}
Nicholas Paldino [.NET/C# MVP] - 15 Jun 2007 01:10 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

>I have an interface which has a bunch of methods defined that all the
>inheriting classes will have to implement.
[quoted text clipped - 25 lines]
> }
> }
Samuel R. Neff - 15 Jun 2007 02:54 GMT
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.

>I have an interface which has a bunch of methods defined that all the
>inheriting classes will have to implement.
[quoted text clipped - 25 lines]
>    }
>}
Mike Labosh - 15 Jun 2007 04:28 GMT
>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.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.