Generics With Type Uknown At Compile Time
The following is an example of a service containing both generic method calls and generic types.
Now suppose that you don't know the actual type of the data until runtime (possibly because the type is retrieved from a DB or from some other service). You need a way to interact with these generics, and Reflection can help you here.
Let's assume that you have managed to get the Type
of the data that you
are handling (such as string
, decimal
, double
, etc...)
and that you have a concrete implementation of the IPropertyService
(perhaps through Dependency Injection).
Once you have that, you can invoke the GetProperty
method in the following way:
As you can see are quite a few steps to be performed, but at the end, through reflection, you end up with a container filled with all the objects that you have required.
Now assume that you want to access to the single properties contained in the IContainer.
You need some way to invoke the C# property Properties
. defined on IContainer
.
Similarly to the MethodInfo
class, System.Reflection has a
PropertyInfo class
that you can use to work with Properties.
The following solution, despite compiling fine, will not work at run-time:
If you try to run this, you will be greeted by the following exception: Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.
After scratching my head for a while, I realized that I should have
first created a generic type at run-time before retrieving the PropertyInfo
:
At this point I have a collection of IProperties
which I can enumerate through.
If needed, since the types in this collection are also generic, I need to repeat the process above
until I reach a fundamental type (such as string
, double
, etc...).
Leave a Comment