Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current type in a static, generic method?

Tags:

c#

generics

I've got an abstract class like this;

public abstract PropertyBase
{
    public static System.Type GetMyType()
    {
      return !!!SOME MAGIC HERE!!!
    }
}

I'd like to subclass it, and when I call the static GetMyType(), I'd like to return the subclass's type. So if I declare a subtype;

public class ConcreteProperty: PropertyBase {}

then when I call

var typeName = ConcreteProperty.GetMyType().Name;

I expect 'typeName' to be set to "ConcreteProperty." I suspect there's no way to do it, but I'm interested if anyone out there knows a way to get this info.

(The particular problem I'm trying to solve is the verbosity of dependency properties in WPF; I'd love to be able to do something like this;

class NamedObject : DependencyObject
{
    // declare a name property as a type, not an instance.
    private class NameProperty : PropertyBase<string, NamedObject> { }

    // call static methods on the class to read the property
    public string Name
    {
        get { return NameProperty.Get(this); }
        set { NameProperty.Set(this, value); }
    }
}

And I almost have an implementation, but I can't quite get the info I need out of my NameProperty class.)

like image 591
Steve Cooper Avatar asked Jan 24 '10 01:01

Steve Cooper


People also ask

How do you find the type of generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

Can you use generics in static method?

Static and non-static generic methods are allowed, as well as generic class constructors. The syntax for a generic method includes a list of type parameters, inside angle brackets, which appears before the method's return type.

Are generics static in Java?

Using generics, type parameters are not allowed to be static. As static variable is shared among object so compiler can not determine which type to used. Consider the following example if static type parameters were allowed.


2 Answers

You can partially achieve (1-level of inheritance deep) using generics:

class PropertyBase<T>
{
    public static Type GetMyType() { return typeof (T); }
}

// the base class is actually a generic specialized by the derived class type
class ConcreteProperty : PropertyBase<ConcreteProperty> { /* more code here */ }

// t == typeof(ConcreteProperty)
var t = ConcreteProperty.GetMyType();
like image 187
LBushkin Avatar answered Sep 22 '22 03:09

LBushkin


The subclassing bit will not work, because a static method is tied to a type. It is a method of a type, not a method of an instance. The subtype does not contain the static methods of a base type, because they are different types and the static method is tied to the base type. Even though the compiler might allow you to call a static method of a base class as through a derived class, it will in reality call the method from the base class. It's just syntax sugar. For the same reason you cannot "override" static methods in subclasses because it would make little sense.

like image 32
Vilx- Avatar answered Sep 22 '22 03:09

Vilx-