Here is a lesson on how you can inspect your generic types.
You know, the thing with generics is that you basically don't know what type you have during compile
time.
Because right here, as you can see, our Configuration manager T is set with a generic type.
So whenever you need to inspect an instance of a generic class, for example, you can use the so-called
reflection namespace.
So you simply go to the top import using system dot reflection.
Right.
In that way we can use typeof to get additional information.
As I said, like the use cases here are really incredible.
Advance I have to mention that like you'll use that when you build your own dotnet frameworks or mock
testing APIs or whatever.
So really advanced stuff.
So here I have an internal class called Configuration Manager.
It's a generic type T.
We have a field here called T loaded configuration.
We have a constructor here.
And we have just a method static one called save config which saves a config of type t.
You have seen all of that before in that section.
So scroll to your void main and then use the reflection by writing down type right.
So call it type equals to.
Now use the type of keyword so that we can get the type information of a specific class, no matter
if it's generic or whatever.
Right.
You could put in many data types and structures.
Let's put in here our configuration manager and specify the type as just empty.
For example, you could also put in a string or whatever type you would like right.
And then you can access this type to get all the information about the generic structure that you have
right here.
I just want to set a breakpoint in light ten.
So when I start the application it will stop that breakpoint just when it got passed.
There we go.
And we can now hover above type and take a look at the information.
Inside.
You can see from which assembly it's coming.
Um, if we have a base type we have attributes.
For example, we can find all the declared events, members fields.
We have a full name here.
You can get really a lot of information.
Is it an array, is it abstract?
Is it sealed?
Is it a class.
You know, is it generic?
Right.
You can see it's generic type set to true and so on.
So you can use this reflection generally in C sharp.
But as I said often used with generics because you need that information.
You know C sharp is type safe right.
We have types and classes in C sharp.
So most of the time you get compile errors while you're still in Visual Studio or when you start your
application.
But with generics where all the types are generic, chances are that you might run if your logic gets
more complicated into scenarios where you want to learn or find out more, or write specific scenarios
for when a generic type is of a specific value.
So for example, hey, if we have a generic type of type string here, we want to do that.
And if the generic type is of type integer, we want to do it that way.
As I said incredible advanced.
In that case you want to find out more about your generics.
And you can do that by using the reflection.
So all of the information about what what's under the hood in C sharp, you can find that out with the
type of keyword.
If you store it inside of type you can inspect it.
Basically one common thing that you do with that during usual human development, right.
Nothing too crazy you would do, for example, something like that.
Uh, you could go ahead and say, if not, let's just create an instance here.
Uh, let's create a string.
For example, string my name equals to Yannick.
Right.
And we could say if my name dot get type you get the type of your in that case variable here is equals
to type of string.
All righty.
That's something you would do.
And you could say hey it's a string right.
This is a simple if condition or if statement that you can write.
So first grab the type the underlying type of your variable which would be string.
Right.
So you use that local variable my name you get the type which will return a string.
In that case it's not a not a string object like it's a more complicated object, but it will be a string.
A represented string is equals to the type of string.
Right?
So as I said, get type.
If you have above that returns a type.
And we have just inspected the type.
It's really complicated.
Lots of information inside.
Right.
The the assembly and so much more.
And you simply compare that with the type of that we have just used before that you can get the information
of a specific data type string in that case.
And this is basically.
Matching.
And you would say, yeah, I have found out and validated that the variable here is of type string.
And you could then write some logic on your own.
So as I said, like reflections are pretty much incredibly advanced and you will rarely find any use
case for using reflections.
But sometimes you might want to check for a type and then you can use it that way you use get type on
your instance, and then you double check if it's a specific type that you are looking for.
Another common field where you will find that is in inheritance, where you also sometimes want to check
with virtual and override.
Hey, if it's a specific type, then do that.
If it's another type, then do that.
This is also a use case where this could become in handy.
Great.
So that's it on C-sharp and Reflections.
If you want to get more information about your classes and generic methods and so on, you can use the
system reflection namespace, especially the type of keyword right here.