| Start |
|---|
Get the members of a type Output (see note about output order further down): We can also use the One thing to note that |
| Get a method and invoke it Get Instance method and invoke it Output: hell Get Static method and invoke it On the other hand, if the method is static, you do not need an instance to call it. Output: 7.38905609893065 |
| Creating an instance of a Type The simplest way is to use the However, even though With |
| Get a generic method and invoke it Let's say you have class with generic methods. And you need to call its functions with reflection. Let's say we want to call the GenericMethod with type string. For the static method you do not need an instance. Therefore the first argument will also be null. |
| Get a Strongly-Typed Delegate to a Method or Property via Reflection When performance is a concern, invoking a method via reflection (i.e. via the This technique can be extended to properties as well. If we have a class named ...and the same can be done for the setter: |
| Get a System.Type For an instance of a type: From the type itself: |
| Getting and setting properties Basic usage: Setting read-only automatically-implemented properties can be done through it's backing field (in .NET Framework name of backing field is "k__BackingField"): |
| Custom Attributes Find properties with a custom attribute - Find all custom attributes on a given property Enumerate all classes with custom attribute - Read value of a custom attribute at runtime Usage |
Create an instance of a Generic Type and invoke it's method |
| Instantiating classes that implement an interface (e.g. plugin activation) If you want your application to support a plug-in system, for example to load plug-ins from assemblies located in This class would be located in a separate dll Your application's plugin loader would find the dll files, get all types in those assemblies that implement |
| Determining generic arguments of instances of generic types If you have an instance of a generic type but for some reason don't know the specific type, you might want to determine the generic arguments that were used to create this instance. Let's say someone created an instance of where so at compile time you don't have any idea what generic arguments have been used to create
But this is not all we want to know. The To enumerate the generic arguments of an instance, we can use the So the call from above ( |
| Get a Type by name with namespace To do this you need a reference to the assembly which contains the type. If you have another type available which you know is in the same assembly as the one you want you can do this: Less efficient but more general is as follows: Notice the check to exclude scanning System namespace assemblies to speed up the search. If your type may actually be a CLR type, you will have to delete these two lines. If you happen to have the fully assembly-qualified type name including the assembly you can simply get it with |
Looping through all the properties of a class RemarksReflection (opens new window) allows code to access information about the assemblies, modules and types at run-time (program execution). This can then be further used to dynamically create, modify or access types. Types include properties, methods, fields and attributes. Further Reading : |
Read Article
https://develop.horje.com/learn/1434/reference