Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a static property of a generic class with reflection?

I have a class (that I cannot modify) that simplifies to this:

public class Foo<T> {
    public static string MyProperty {
         get {return "Method: " + typeof( T ).ToString(); }
    }
}

I would like to know how to call this method when I only have a System.Type

i.e.

Type myType = typeof( string );
string myProp = ???;
Console.WriteLinte( myMethodResult );

What I've Tried:

I know how to instantiate generics classes with reflection:

Type myGenericClass = typeof(Foo<>).MakeGenericType( 
    new Type[] { typeof(string) }
);
object o = Activator.CreateInstance( myGenericClass );

However, is this proper to instantiate a class since I am using the static property? How do I gain access to the method if I can't compile time cast it? (System.Object does not have a definition for static MyProperty)

Edit I realized after posting, the class I'm working with is a property, not a method. I apologize for the confusion

like image 280
James Avatar asked Jul 09 '12 14:07

James


People also ask

How do you call a generic using reflection?

The first step to dynamically invoking a generic method with reflection is to use reflection to get access to the MethodInfo of the generic method. To do that simply do this: var methodInfo = typeof(ClassWithGenericMethod). GetMethod("MethodName");

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.

How to call static property in c#?

In C#, static means something which cannot be instantiated. You cannot create an object of a static class and cannot access static members using an object. C# classes, variables, methods, properties, operators, events, and constructors can be defined as static using the static modifier keyword.

Can properties be static?

#259 – Static vs. Instance PropertiesYou can also define static properties, which are properties that have a single value for the entire class, regardless of the number of instances of the class that exist. You can read and write a static property even if no instances of the class exist.


2 Answers

The method is static, so you don't need an instance of an object. You could directly invoke it:

public class Foo<T>
{
    public static string MyMethod()
    {
        return "Method: " + typeof(T).ToString();
    }
}

class Program
{
    static void Main()
    {
        Type myType = typeof(string);
        var fooType = typeof(Foo<>).MakeGenericType(myType);
        var myMethod = fooType.GetMethod("MyMethod", BindingFlags.Static | BindingFlags.Public);
        var result = (string)myMethod.Invoke(null, null);
        Console.WriteLine(result);
    }
}
like image 116
Darin Dimitrov Avatar answered Oct 21 '22 15:10

Darin Dimitrov


Well, you don't need an instance to call a static method:

Type myGenericClass = typeof(Foo<>).MakeGenericType( 
    new Type[] { typeof(string) }
);

Is OK... then, simply:

var property = myGenericClass.GetProperty("MyProperty").GetGetMethod().Invoke(null, new object[0]);

should do it.

like image 25
Kek Avatar answered Oct 21 '22 14:10

Kek