Reflection introduction for .NET Core (C#)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dotnet course central in today's video I am going to talk about reflections in.net core reflections is a feature provided by dotnet it's a feature using which we can reflect on an assembly to get the types and other information by reflect I mean we can take an assembly and then from the assembly we can figure out what are the different types defined in the assembly and then inside of each type what are the different methods properties and fields and then we can also invoke those properties and fields creating an object and we can do all of these using reflection and not essentially doing a new on that type so to demonstrate that I'm going to create a new dotnet core console application and I'm going to name it as reflection dot demo now I'm going to get rid of this default console statement once the project is created first let us create another project and this time I am going to select a class library and I'm going to name it as print all so this class library is going to have a class called let's rename it and let's call it got something let's say this class is responsible for doing printing but beyond printing also we will have some other functions so let's define few functions and properties in the class so first one is a public method and this one is just going to be print and this one is just going to do a console dot write and it's going to print out printing from print and then next one I'm going to create another public method and this time it is going to return a string and it's going to say get name and it's going to return a variable name and this name is going to be a class level variable so I'm going to say private string name now after that now I'm going to create method and this time it's going to print the name said it's a print name and it's going to have a console dot write line it's going to say a name set as name and then I will have another method which is going to be void print and this one is going to take a name to print as a argument so here we'll have console dot write line and we're going to say name passed name so right now we have different variations of public method one is a void doesn't return anything and prints out a constant string another one returns a private method then the next one prints out the private variable that was set and one of it that takes a parameter and prints that out this is the five variations then another one I'm going to have just a private method and this is also void and we print private and here we're just going to say console dot write line off printing from private and then I'm going to have couple of properties so public string name and it's going to return the name and another one public static because so far I declared all instance methods so now let's declare a static method also and let's call the static name static property name this has to be string that's the return type so now we have this class defined and this class is defined inside of this assembly and this is the path for this assembly so now I'm going to fast rebuild this so that the assemblies are created in the bean debug folder so this is where the assemblies are created as you can see print all is created here so we can copy this path and then let's start using reflection so for starting reflection the first thing we have to do is get a handle of the assembly so for that I can say a bar assembly equal to assembly dot load from and here I can provide the path of the assembly which is this path I just copied and then after I do that let's first start with what are the different types define each side of the assembly and for that I'm going to do a for each and type in assembly dot care types so the gap types is going to give all the types in defined inside of assembly and then what I'm going to do is I'm just going to do a console dot write line and here I'm going to say type is type dot name right so that's the type of the assembly and then let me create partition because I'll be doing more printing so fast I'm going to get the type defined inside of assembly for this one we'll just have one time which is a custom print and then next thing is I want to print all the methods all the fields and all the properties first let me start with the fields of I'll do a for each field which is the field at the kite level type dirt get fields and get filled method has an overload which is binding flags and as you can see it says finding flags is a bitwise combination of enumeration values right so we are going to say binding flag start we want nonpublic because if you remember the name is a private field so we'll say non public and then we'll say we need instance because it's an instance property we get it to the next line and then we are going to do another operation and we are going to say a binding Flags taut declared only means the items which are declared inside of this type only and then I'm going to do console dot write line and here I'm going to say field field dot name so that's where we define the field and I'm missing a bracket here so I'll close this ok ok and after these printing the fields let create delineation and now next thing I'm going to do is I'm going to print all the functions or methods so again I'll do for each method in type thought get methods and here I'm going to use the same set of binding flags because I want all the instance because I don't have any static methods I have static property so for this one you know I don't need it but you get the idea and then next I'm going to do similarly here I'm going to do console dot write line and I'm going to say method is now when we print this you will see unexpected behavior which you might know already but it'll be interesting to see what this prints out and we'll discuss why it happens and how to avoid that so after we print all the methods now it's time to print all the properties you can say property in type dot get properties here we don't need any binding flag because you know we don't have to and we can say console dot write line it's like before property is property dot name so now here we printed out the type using reflection we got the types from the assembly for each type will print the type name and then again accusing reflection will call get fields printer all the fields print out all the methods and print out all the properties so let's run this and now we can see for type we see custom print for field we see name for method we see only print private we don't see anything else and then for property we see both name and static name now let's figure out what might have gone wrong with the method so for method the mistake we did is we copied this one so we say non public so let's remove this let's make it as public rerun this now all the public method should come but now you will see an interesting fact we got a print we got get name we got print name print is it all expected but then we are going get underscore name which is the internal name of the name property now we want to escape that because we don't want property to be shown as a part of the method because they'll cause a problem if we want to invoke those method so now how to do that for that what we are going to do is here we are going to add a condition here so we are going to say it dot where and here we get the method as the parameter and here we can say there's a property declared which is a special name and the special name indicates whether the method has a special name which essentially says whether it is the properties or not because only properties have special names these are defined by the compiler so now if I run these I will see only the get name so I have not off specially and now if I run I can see exactly what I expect so I see get the type filled all the methods and the properties now well this is interesting but it's going to be way more interesting if we can invoke the methods so for invoking first we have to create an instance of the type so now how do you create an instance of the type we cannot say a new of the type that's not going to work right so here we are going to use a class called activator so you're going to say divider dot create instance this is the method and when we create instance we're going to pass the type for which we want to create instance and also it has one of the other this takes assembly name type name you can use that also it takes if it is non public and then there is another parameter which takes theorem which is an array of object which you will be using if it has a constructor which takes arguments in that case you will use that for us we don't have a constructor so we don't have to worry about it so your create instance and this is going to give us the instance of this type yes so now we have the instance now we have to call those methods on the instance so fast let's say we want to set the field value so that we can get inside of this method when we are calling like print name we should be able to let me just just for clarity let's say this stuff name here also this dot name so that it doesn't look confusing with this name here which is this parameter okay so now if we set the field here once we set the field when the print name method will be called it will print the name that we have said and also the getname method will return the name that was set so for selling the field what we are going to do is field dot set value and you can see the first parameter is the object basically the instance whose field it is so this is the instance that we created and then for value we have to pass an object so we are going to name it as Frodo so we said the name now the name is set and now let's try to call all methods now for each method we want to call the method now while calling the method there are a couple of things we have to figure out right because if you see one of the method is private which we did not get yet because we did not hand we just had public we did not have non public so let's add that also its binding flags or non public so that we get both public and non-public and then and this is a fundamental difference between using reflection versus calling an object directly creating an instance is that when you use reflection you can access non-public method also which is pretty dangerous now when we print this but before printing before setting everything let's just run and you show the non-public methods if I run this project now you can see the private print is also showing out the print private method so now let's start again some comment this so we are setting the value and now for each method now one of the method is different than this rest of the other method this method takes a parameter and also this one returns a well so we have to treat them differently so for that first what we're going to do is we're going to check to put a if condition so we're going to say if method dot get parameters so it will return if there are parameters so we can say if dot parameters dot length is greater than zero then behave differently in this case we're going to say method dot and for calling a method we are going to use invoke and the first parameter of invoke as you have seen is the instance this will pass the instance and the second parameter is an array of object so we can do like this and here we are going to pass a value now here this is the only method which takes parameter it takes a string name so we need to say bilbo so this is the one if it has parameter then the next one what is the next condition the next condition is if it returns something we want treat it differently so if method dot return type the return type is going to be void and that's a type so we can say return type dot name it's not equal to void then what we are going to do is for return value is equal to method dot invoke instance and for the second parameter just passed now and then here we can do console dot write line and we can say return value from method and we can say return value this is done so if it has parameter if it is not void and then plus finally we just invoke the math method or invoke and here we pass instance and now that's all the method and then finally for property we can save our property value is equal to property dot cat get value and here we'll pass the instance and then we can say console dot write line property value property value so now let's go to the code again so first we did assembly door load from we loaded from an assembly there is another method in assembly calls just load which can load an assembly by name but in that case the assembly has to be somewhere accessible and that will be if they are in the same bin folder so we can do that as well then we are doing assembly target types we're getting all the types and then we are doing an activated or create instance passing the type to create an instance of the time after that we got the fields and then we are setting the value of the fields this is a non public field and you can see we can set the value of a non public field using reflection which is very different from if you are creating an object by using new and then we are going through all the methods appending the name of the methods and then we are dealing with the method if it has parameter then we a spirometer if the return type is not equal to void then we get the returned I have been printed otherwise we are just invoking the method we deal with all the three cases that we have and then finally we get all the properties print the name of the property and then get the value of the property and print it out okay I have to have else if else condition otherwise this method will be invoked in all the cases so let's just put it this way okay so you should ensure that this method is not called every time otherwise you'll be called every time so now let's run the application now once we run the application we have all the responses which is pretty cool it's a fast method print and this is printing from print and this is exactly what we had for method print it was printing from print then we are calling get name and return be look from the method is Frodo proto is the value that we set up here in the private field and the get name returns a private field so it is working as expected one after that is print name and his name said is Frodo because print name says name said is this dot name which is Frodo this working is expected war effort that we're calling method print the overload passing a parameter we passed parameter Bilbo and it says name passed Bilbo name passed Bilbo this is working as expected and the last method is the private method and says printing from private so as we can see all of these are working as expected and finally we call the static and non-static property and for both of them forestry property it is returning the name Frodo which is the name we set as you can see here it returns name for the non static and for the static property it is just returning the static property name which we are returning from here now one thing I forgot to mention here is that when we call get value the instance we are passing but instance will be ignored if this property is staring you can pass it you don't have to pass it if you are calling a static property same thing goes for method in book also if it is an static method the instance will be ignored that's all I wanted to cover today if you liked this video please give me a thumbs up and if you want me to cover anything more when it comes to reflection please provide a comment and I can cover it in the next video because this is just scratching the surface in terms of what all you can do with reflection and if you have been following my channel and if you think you are getting value out of my channel please subscribe to it if you have not already subscribed thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 3,390
Rating: undefined out of 5
Keywords: reflection, reflection .net core, reflection c#, reflection asp.net core, dynamic object creation, dynamic object creation c#, runtime method invocation c#, c# runtime method invocation, c# dynamic object creation
Id: MqJ_JjCV-9M
Channel Id: undefined
Length: 19min 56sec (1196 seconds)
Published: Sun Jun 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.