Python style class style for C #?

Is there a way to do what classmethod does in Python in C #?

That is, a static function that receives the Type object as an (implicit) parameter, depending on which of the subclasses it used.

An example of what I want, roughly,

 class Base: @classmethod def get(cls, id): print "Would instantiate a new %r with ID %d."%(cls, id) class Puppy(Base): pass class Kitten(Base): pass p = Puppy.get(1) k = Kitten.get(1) 

Expected Result:

 Would instantiate a new <class __main__.Puppy at 0x403533ec> with ID 1. Would instantiate a new <class __main__.Kitten at 0x4035341c> with ID 1. 

(The same code is in codedepe here.)

+6
python c # class-method
source share
5 answers

Basically, you can write something like this:

 class Base { public static T Get<T>(int id) where T : Base, new() { return new T() { ID = id }; } public int ID { get; set; } } 

Then you can write var p = Base.Get<Puppy>(10) . Or, if you feel masochistic, you can write Puppy.Get<Puppy>(10) or even Kitty.Get<Puppy> ;) In all cases, you need to pass the type explicitly and not implicitly.

Alternatively this also works:

 class Base<T> where T : Base<T>, new() { public static T Get(int id) { return new T() { ID = id }; } public int ID { get; set; } } class Puppy : Base<Puppy> { } class Kitten : Base<Kitten> { } 

You still need to pass the type back to the base class, which allows you to write Puppy.Get(10) as expected.

But is there any reason to write it like this when var p = new Puppy(10) is just as concise and more idiomatic? Probably not.

+1
source share

I think you want to take a look at generics.

MSFT Guide:

http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx

+3
source share

Depending on the context, you either want to generate or virtual methods.

+2
source share

This is equivalent to the class methods in Object Pascal. (.Net implementation would be RemObject oxygen).

However, although class references and, therefore, virtual class methods or seemingly static methods that can access some state at the class level are good there, the original platform is there, I donโ€™t think they make sense for .Net or with#.

I have been using Oxygene for several years, and I never need class references.

Not because I did not know how to use them. In the "original" ObjectPascal platform, the native Delphi, I used them all the time. But because .Net and BCL do not have real support for them, so you have no advantages using them. While platforms like python or native Delphi support them in their libraries.

You obviously don't need a static method, what you want is something that can have state and / or supports inheritance. Thus, either the factory, or, in your case, the designer can also be wonderful.

0
source share

Since C # is statically compiled, calling Puppy.get() allowed at compile time, for example. as you know, it points to Base.get() , which means that the generated CIL (Common Intermediate Language) will have a command to call Base.get() :

 .method private hidebysig static void Main() cil managed { .entrypoint // Code size 9 (0x9) .maxstack 8 IL_0000: nop IL_0001: ldc.i4.1 IL_0002: call void Base::get(int32) IL_0007: nop IL_0008: ret } 
0
source share

All Articles