Re: static methods in an interface
Christophe Fouquet <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <[email protected]> |
What you are trying to do is not possible at compile time.
I have the same situation and I check on application start up that certain
classes (same base) have implemented a certain set of static methods.
I do this using reflection and hold on to the MethodInfo for speed later.
It is not perfect but it works. You can then do things like this
Void MyHelperToCallAStatic(Type t)
{
MethodInfo mi = t.GetMethodInfo()....
mi.Invoke(); // etc...
}
I use this in ASP.Net to make sure a page had a static string GetLink()
method. Then, another pages can do this
Response.Redirect(SomeOtherPageClassName.GetLink());
To build a dynamic list of links to pages, you then need a Type[] and compute
the links via the MyHelperToCallAStatic().
CF
Christophe Fouquet
Sr Software Engineer
1300 Oakbrook Dr.
Norcross, GA 30093
[770] 453 7489 tel
[770] 448 3202 fax
[email protected]
"The cure for boredom is curiosity. There is not cure for curiosity"
-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Peter Osucha
Sent: Friday, August 25, 2006 8:36 AM
To: [email protected]
Subject: Re: [DOTNET-CX] static methods in an interface
Thanks, Peter, for that explanation.
What I was trying to do was to force some classes marked 'internal' in a
project to all support the same methods. I guess I'll do some rethinking...
Peter
> -----Original Message-----
> From: Discussion relating to the specifics of the C# and
> Managed C++ languages [mailto:[email protected]]
> On Behalf Of Peter Ritchie
> Sent: Thursday, August 24, 2006 4:33 PM
> To: [email protected]
> Subject: Re: [DOTNET-CX] static methods in an interface
>
> An interface is a definition of what a class instance should
> make publicly available. Since an instance is not static,
> you cannot declare static members in an interface.
>
> Also, since static members are not in any virtual table
> because they are not related to any particular instance, you
> cannot make a static member virtual--ergo you cannot mandate
> a class to implement specific static members to fulfil derivation.
>
> On Thu, 24 Aug 2006 15:10:12 -0400, Peter Osucha
> <[email protected]> wrote:
>
> >I have an interface defined with some methods. I have two
> classes that
> >implement this interface - however, I want the classes to
> implement the
> >methods statically.
> >
> >I tried setting the method definition to 'static' in the
> interface but
> >apparently, that's not allowed (I get an error 'The modifier
> 'static'
> >is
> not
> >allowed for this item').
> >
> >If I just try to use the 'static' modifier in the class that
> implements
> the
> >interface, I get another error telling me that the class does not
> implement
> >the interface method (which doesn't have the 'static'
> modifier). What
> >is going on here and why am I not allowed to create a
> 'static' method
> >this
> way?
> >How might I accomplish what I am trying to do, please?
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com