Very good videos, all of them, I'm trying to refresh some C# skills after two years of doing nothing with it and your videos are helping me a lot. Even more, my first language is not English and I can understand you loud and clear. I stuck thinking in the minute 13 and I remembered that there is another difference between the use of the typeof operator and the GetType method, and it's that we can use GetType method only using instances of the types, whereas the typeof operator can be only used on the type itself. Great job, thanks for your time and effort.
Good tutorial man, Java is my primary programming language and now i'm learning C# for two of my other papers. They are both similar in most areas however the more you learn about C#, you come across new concepts like Attributes, Delegates, Events and Reflections which are all new & confusing but they have their use. Hope to understand these concepts more and you are doing a great job on explaining these C# concepts. God bless! :D
I think it's good to note a paradigm you go through which is a common paradigm when working with reflection: You frequently use reflection to work through some variables and set up a type of structure in variable-form which represents what you want to do. For example, you get a type, you reflect on its members, you select a property or method. And that is all just plain'ol'reflection. But what ends up happening is that you then need an INSTANCE to apply your reflection template to. Part one is just mapping out what kind(s) of type(s) and how their members are structured and the type of data you may want to invoke or read from. Part two is to give an instance of matching implementation to your structure so that getting-value or invoking-members actually becomes meaningful. Understanding those two separate parts helps to use reflection in my mind.
I'm a little bit confused by the ".Count()" call in "type.GetCustomAttribute().Count()". Does that mean a type can have multiple times the same atttribute?
First of all I want to say that your videos are awsome, you are confident, you explain it clearly and you know exactly what you want to do. Second, I understood what you are doing, however I did not understand why you are doing it. It feels like you are itching your left ear with your right hand. Why would you execute a method through the reflection if you can do it through the object you created? (just an example)
Yagnesh Cangi please help with error Error 3 The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(System.Type, bool)' cannot be used with type arguments
This is all cool and stuff, but what's the benefit of retrieving the type of Sample and invoke the method through the type, with the variable sample specified, through Reflections, rather than just go "sample.MyMethod();"? Attributes I get, but the rest of this tutorial's content just seems like extra steps, that cost more resources, to do something that could be done a lot easier.
+morphman86 Long-Story short: Reflection is bad mkay. Of course, like all tools in our toolbox, some of them are necessary sometimes. Sort of like the Star-Screwdriver. You rarely need to use it, but you should have one... and some people use them for everything even though they have a dam hammer right next to them.. STOP USING YOUR STAR-SCREWDRIVER AS A HAMMER!!!!. Sometimes you have no clue whatsoever about what classes you are using. Take .NET Web API For example: If I wanted to create some documentation or auto-implementation of some of my ApiControllers... but developers are constantly adding, removing, and altering the ApiControllers, I cant very well have hard-coded implementations for every single one. There could be anywhere from one to a hundred and they constantly change. So instead I use reflection to get the Assembly that contains my ApiControllers... then I get all classes that Implement ApiController... then I can continue on from there. Using this method, I could very powerfully create utility libraries that help me organize and construct code. Granted: I havent personally ever used reflection to handle Majorly Main-stream business logic. Though I do use it frequently for code maintenance, DLL Injection, and other fun stuff. What if MY Application wanted to open up a copy of a form in YOUR Application... I can use reflection to load in your Assembly and pull out what I want. Muy Importante... but not used as much as a hammer.
Reflection is great for more advanced things like dependency injection. With it you can do complex things like auto initialize all classes that inherit an interface dynamically based off the namespace. So instead of configuring the container to add the class each time you make a new one, reflection will automatically add it to the container as long as it meets the condition you provided.
Hey thanks a ton... if anyone having error in attribute calling try var types = assembly.GetTypes().Where(a => a.GetCustomAttributes(typeof(MyClassAttribute), false).Count() > 0);
Rakesh Gaur i got error . please help Error 3 The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(System.Type, bool)' cannot be used with type arguments
For fetching specific type you are using Attributes and Genrics But in Reflection also you can get it by : var sampleType=typeof(Sample); What's the benifit of generics and Attributes over this ?
The explanations are so clear that I'm willing to forgive you for using the light mode.
I'm glad I found this tutorial first, this is exactly what I needed to know. Thanks!
Very good videos, all of them, I'm trying to refresh some C# skills after two years of doing nothing with it and your videos are helping me a lot. Even more, my first language is not English and I can understand you loud and clear. I stuck thinking in the minute 13 and I remembered that there is another difference between the use of the typeof operator and the GetType method, and it's that we can use GetType method only using instances of the types, whereas the typeof operator can be only used on the type itself.
Great job, thanks for your time and effort.
Good tutorial man, Java is my primary programming language and now i'm learning C# for two of my other papers. They are both similar in most areas however the more you learn about C#, you come across new concepts like Attributes, Delegates, Events and Reflections which are all new & confusing but they have their use. Hope to understand these concepts more and you are doing a great job on explaining these C# concepts. God bless! :D
It's a pleasure to listen to this, thank you for sharing.
I think it's good to note a paradigm you go through which is a common paradigm when working with reflection:
You frequently use reflection to work through some variables and set up a type of structure in variable-form which represents what you want to do. For example, you get a type, you reflect on its members, you select a property or method. And that is all just plain'ol'reflection.
But what ends up happening is that you then need an INSTANCE to apply your reflection template to. Part one is just mapping out what kind(s) of type(s) and how their members are structured and the type of data you may want to invoke or read from. Part two is to give an instance of matching implementation to your structure so that getting-value or invoking-members actually becomes meaningful.
Understanding those two separate parts helps to use reflection in my mind.
man, you are the best teacher ever. Thank you so much. Thump up + subscribed
I was hanging about using reflection :D, your video was great and I understood the concept. Thank you!
Really helpful and concise tutorial as well as a clear explanation of reflections. Thanks Clint!
You are an amazing instructor!
You sound like Saul Goodman from "Better Call Saul", Thanks!
Thank you so much, It will be very helpful to create frameworks. I am not pro in C# but I understood clearly. Liked the video and subscribed
Great sharing!
Many useful!
Great thanks!
i really don't understand, why not just you know...run the method the normal way?
Frameworks
Good point, especially when are working with Generics, Reflection is very useful. I think EF also uses Reflection behind the scene.
Great explanation! Thank you
What is the benefit of reflection, why would we use that in our code knowing it needs to write more code?
Why those guys from Microsoft can't explain in their videos like you do ? Good job!
man you are the best .... thanks a lot .... please keep it up
I'm a little bit confused by the ".Count()" call in "type.GetCustomAttribute().Count()". Does that mean a type can have multiple times the same atttribute?
Fudging awesome! Thanks
how does this work with protected and private methods though?
What are the things that you put inside of your custom AttributeClass?
how do you resist the light mode
First of all I want to say that your videos are awsome, you are confident, you explain it clearly and you know exactly what you want to do.
Second, I understood what you are doing, however I did not understand why you are doing it. It feels like you are itching your left ear with your right hand. Why would you execute a method through the reflection if you can do it through the object you created? (just an example)
good tutorial, i learned a lot. one thing is not clear for me though. How come the lambda returns the types or methods instead of a bool?
deep dark fantasies
Wonder if u actually can GetAnExternalExecutingAssembly ? that wuld be rly cool for reversing and stuff .
Great Video. Thanks a lot
Super fantastic brain candy. Thanks!
hey, rlly cool, but how can i invoke the method Parse(String s) from double?
You can use .Any() instead of .Count()>0
Yagnesh Cangi please help with error
Error 3 The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(System.Type, bool)' cannot be used with type arguments
+Yagnesh Cangi Shh... he isn't to Linq yet. Lol.
great tutorial!
Can you provide the code, please?
This is all cool and stuff, but what's the benefit of retrieving the type of Sample and invoke the method through the type, with the variable sample specified, through Reflections, rather than just go "sample.MyMethod();"?
Attributes I get, but the rest of this tutorial's content just seems like extra steps, that cost more resources, to do something that could be done a lot easier.
+morphman86 Long-Story short: Reflection is bad mkay.
Of course, like all tools in our toolbox, some of them are necessary sometimes. Sort of like the Star-Screwdriver. You rarely need to use it, but you should have one... and some people use them for everything even though they have a dam hammer right next to them.. STOP USING YOUR STAR-SCREWDRIVER AS A HAMMER!!!!.
Sometimes you have no clue whatsoever about what classes you are using. Take .NET Web API For example:
If I wanted to create some documentation or auto-implementation of some of my ApiControllers... but developers are constantly adding, removing, and altering the ApiControllers, I cant very well have hard-coded implementations for every single one. There could be anywhere from one to a hundred and they constantly change.
So instead I use reflection to get the Assembly that contains my ApiControllers... then I get all classes that Implement ApiController... then I can continue on from there.
Using this method, I could very powerfully create utility libraries that help me organize and construct code.
Granted: I havent personally ever used reflection to handle Majorly Main-stream business logic. Though I do use it frequently for code maintenance, DLL Injection, and other fun stuff. What if MY Application wanted to open up a copy of a form in YOUR Application... I can use reflection to load in your Assembly and pull out what I want.
Muy Importante... but not used as much as a hammer.
Suamere Thank you, I think I understand now.
Also, I think I should get a hammer and stop using my star-screwdriver for nails...
Reflection is great for more advanced things like dependency injection. With it you can do complex things like auto initialize all classes that inherit an interface dynamically based off the namespace. So instead of configuring the container to add the class each time you make a new one, reflection will automatically add it to the container as long as it meets the condition you provided.
I was hoping you'd also cover reflection.Emit, but great video anyways, Thanks!
Hey thanks a ton... if anyone having error in attribute calling try
var types = assembly.GetTypes().Where(a => a.GetCustomAttributes(typeof(MyClassAttribute), false).Count() > 0);
Rakesh Gaur i got error . please help
Error 3 The non-generic method 'System.Reflection.MemberInfo.GetCustomAttributes(System.Type, bool)' cannot be used with type arguments
For fetching specific type you are using Attributes and Genrics But in Reflection also you can get it by : var sampleType=typeof(Sample);
What's the benifit of generics and Attributes over this ?
get juice rocked!!!!!!!!!!!!!!!!
Invoke have NullReferenceException