Monday, January 27, 2014

C# .Net: Get the value of a static property of a generic type

I did a base type for a singleton pattern and used a generic type later to refer to a inherited type. Here is how I got access to the Instance method:

instance = (T)typeof(T).GetProperty("Instance"BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public).GetValue(nullnew object[] { });

This will call the base class Instance property and return the singleton instance of the inherited T class.

It's a bit hard to explain in words... I still hope it helps someone ;)