implementing interfaces with static methods

  • Thread starter Thread starter mdc
  • Start date Start date
M

mdc

Hi,

Is there any way to implement an interface as a static method in C#? If
not, is this a bug?

Micheal.
 
The interface itself determines whether the method should be static or
instance. If an application had an instance of an object with interface
IFoo, and it wanted to call IFoo.X() on the object, just imagine what would
happen if the class implemented IFoo.X() as a static function. You can
declare that an interface member is static in the interface definition, but
you have implement it the way it is declared. This is correct behavior.

Chris
 
Patrick Steele said:
No, you can't do it. And no, it's not a bug. Since an interface is
implemented by an instance of an object, how could you have a static
member of an instance? :)

The one argument I've seen for this is that it would give compile-time
safety for a member which would then be called with reflection. As in,
it would prevent people from thinking they'd implemented everything
they needed to for a certain type of plug-in, when actually they'd
forgotten a static method which was required. The same could be said of
constructors.

My opinion on whether or not that would be a good idea varies quite a
lot :)
 
Hi Chris,

The problem is that c# (I don't know about CLR) doesn't have support of the
full conception of meta-classes.
It support this idea partially. When we declare a class we actually decalre
two types: one is the given class and the second is the meta-class for the
given class. The meta-class' members are all members declared as static as
well as all construcotrs. The one single object of this meta-class type is
created when the class gets loaded (static constractors is executed which is
evidence of existance of instance of the meta-class) and the name of this
objects is the name of the class itself.
So, calling a static method ClassA.X() should be considered as calling the
method X of the singleton instance of the meta-class of the ClassA.
Creating objects of given class obj = new ClassA(...) can considered as a
short-cut for obj = ClassA.CreateInstance(....) where CreateInstance methods
are all constructors of a class.
What we miss here is a type *refrence to a meta-class*. Since c# doesn't
support defining variables of type *reference to a meta-class* having
virtual static methods, static interfaces, etc doesn't make any sense.

For instance Delphi is a language, which supports meta-class conception. In
Delphi we can declare variables of type *reference to a meta-class* that's
why we have virtual constructors for example.

So, I think that this is more conceptual rather than technical issue. I
believe that CLR has the whole inforamtion and structures internaly to make
having virtual constructors, virtual satic members, static interfaces, etc
posible.
B\rgds
100
----- Original Message -----
From: "Chris Capel" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Thursday, July 24, 2003 3:37 PM
Subject: Re: implementing interfaces with static methods
 
"The problem is that c# (I don't know about CLR) doesn't
have support of the full conception of meta-classes."

The CLR supports it. It can be done in ILASM--I haven't
found a sample, but according to the only book on ILASM
that I've read, it can be done.

Regards,

Javier
 
Javier Estrada said:
"The problem is that c# (I don't know about CLR) doesn't
have support of the full conception of meta-classes."

The CLR supports it. It can be done in ILASM--I haven't
found a sample, but according to the only book on ILASM
that I've read, it can be done.

Hmm... I've just modified the IL generated from a normal interface to
make a method static, ie

..method public hidebysig newslot static abstract virtual
void DoSomething() cil managed
{
} // end of method Foo::DoSomething

and compiling against it, it's as if the method doesn't exist.

Note that the spec (ECMA 335) does say (in section 11, partition 1):

<quote>
CLS-compliant interfaces shall not define static methods, nor shall
they define fields (see
clause 8.9.4).
</quote>

Then again, in 8.9.4 it says that an interface *may* define a static
method (presumably if it doesn't mind not being CLS-compliant) but it
doesn't go into details of what that really means.
 
Back
Top