
CodeProject – Reflection in C# Tutorial.Additional Resources and Tutorialsįor more information, including some helpful tutorials, visit the following resources: Other uses for Reflection include constructing symbol tables, to determine which fields to persist and through serialization. Use CustomAttributeData to find out information on custom attributes or to review attributes without having to create more instances.Use PropertyInfo to get the declaring type, reflected type, data type, name and writable status of a property or to get and set property values.Use Assembly to load modules listed in the assembly manifest.Use ConstructorInfo to get data on the parameters, access modifiers, and implementation details of a constructor.Use EventInfo to find out the event-handler data type, the name, declaring type and custom attributes.Use MethodInfo to look at information such as parameters, name, return type, access modifiers and implementation details.
Use Module to get all global and non-global methods defined in the module. When you parse these tables, you can retrieve an assembly’s types and attributes. Within the PE file is mainly metadata, which contains a variety of different tables such as a: When you consider that an assembly is a logical DLL or EXE and a manifest is a detailed overview of an assembly, then it makes sense that a PE (portable executable) file for CTS would have the extension of. With the type, you can get three static types which allow you to load an assembly directly: In this instance, you can ask the user to enter the assembly name and type during run time so the application can load the appropriate assembly. To illustrate, you might not know which assembly to load during compile time. Late bindings can also be achieved through reflection. You can parse the corresponding metadata tables to look through these items: When you use this class, you can find the types used in a module and namespace and also determine if a given type is a reference or value type. The main class for reflection is the System.Type class, which is an abstract class representing a type in the Common Type System (CTS). NumberPropertyInfo.SetValue(calcInstance, 10.0, null) set value of property: public double Number get value of property: public double Numberĭouble value = (double)numberPropertyInfo.GetValue(calcInstance, null) PropertyInfo numberPropertyInfo = calcType.GetProperty("Number") Object calcInstance = Activator.CreateInstance(calcType) Īnd access its members (the following examples illustrate getting values for the public double Number property): // get info about property: public double Number Type calcType = testAssembly.GetType("Test.Calculator") Then, you can use reflection to load the Test.dll assembly: // dynamically load assembly from file Test.dllĪssembly testAssembly = create an instance of the calculator class: // get type of class Calculator from just loaded assembly To access the sample class Calculator from Test.dll assembly, the Calculator class should be defined as the following: namespace Test This is how you would create instances of DateTime class from the system assembly: // create instance of class DateTimeĭateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime)) You first get the “type” object, then use the type to browse members such as “methods” and “properties.” Implementing reflection in C# requires a two-step process. The above example results in the following output: System.Int32 #Java reflection use case how to
This example illustrates how to use the static method GetType to find the Type of a variable: // Using GetType to obtain type information: Reflection can be used to create applications called type browsers which allow users to select types and then read the data provided about them. When you write a C# program that uses reflection, you can use either the TypeOf operator or the GetType() method to get the object’s type.
You can compare Reflection to C++RTTI (Runtime Type Information), except that it has a much wider swath of capabilities. For example, you can get all members of the object by typing “.” before an object when viewing your Visual Studio editor IntelliSense.Ī program reflects on itself when it extracts metadata from its assemblies, then uses it to modify its own behavior or inform the user. You need to use Reflection when you want to inspect the contents of an assembly. To understand reflection, there are a few basics you should understand about modules, types, and members: