Was this helpful?
This is in continuation to Reflection in .NET - Part 3
If ReflectionPermission is not granted to the code, it can access only the public members of the loaded assemblies. The code having the appropriate ReflectionPermission has access to non-public members of any loaded Type. This includes access to Module and Assembly.
The caller needs to have ReflectionPermission and then only it can reflect on private fields, methods, constructors, and properties. For example, before FieldInfo.SetValue() sets the value of an underlying field reflected by a FieldInfo instance, it ensures whether the user has appropriate ReflectionPermission.
If the code is fully trusted, access restrictions are ignored. A fully trusted code is allowed to access and invoke the private methods, constructors, properties, and fields through reflection. However, if the code is from an untrusted source, ReflectionPermission makes it possible to enumerate the types, retrieve type and member information, and invoke methods that would otherwise be inaccessible.
Since ReflectionPermission allows access to class members and information that is not intended for public access, it is advisable that ReflectionPermission should not be granted to the Internet code. It is granted only to trusted code.
It is the principal class in the System.Reflection namespace. For the purpose of reflection, .NET has provided the Assembly class that encapsulates the information about a .NET assembly. In other words, its instance represents a .NET assembly.
You can load an assembly at runtime by making a call to the Assembly.Load() static method. You can pass to this method an AssemblyName object that encapsulates information about the assembly's unique identity such as its name, version number etc. The Load() is an overloaded method that also accepts a full name of an assembly, also known as the display name.
Use LoadFrom() method if you want to obtain an Assembly object corresponding to the file lying on a hard drive. This method accepts the file name and path of the assembly and dynamically loads it and returns the corresponding Assembly object.
You can call the GetExecutingAssembly() method if you want to obtain an Assembly object that represents the currently executing assembly.
Once you have an Assembly object in memory, you can use its properties and methods to query for information in the assembly. It has the properties such as FullName, Entry Point, Location. EntryPoint returns the entry point of the assembly. Location returns the physical name and location of the assembly. FullName returns the full name of the assembly, also known as the display name. FullName includes information about the version number, culture, and public key token.
Use GetTypes() if you want to retrieve all the Types defined in an assembly. It returns an array of Type objects. Use GetType() if you want to retrieve a Type object that represents a specified type.
Use GetModules() if you want to obtain all the modules listed in an assembly manifest. It returns an array of Module objects. GetModule() returns the specified module from the assembly.
Use Module.GetType() if you want to obtain a type from a specific module. If you want to obtain a type from an assembly, irrespective of which module it is in, you need to make a call to Assembly.GetType() method.
Loading an assembly given its full name
Finding Types in a running program
Using the Reflection API, a running program can obtain the information about itself, for e.g. it can find all the types in the currently executing assembly and then it can retrieve all the methods in each type.
Binding Flags enumeration
It is a set of flags that controls the way in which the search for the members and types is conducted by reflection. It has the values like
a. Public: indicates that the public members are to be included in the search.
b. NonPublic: indicates that the private, protected, and internal members are to be included in the search.
c. Instance: indicates that the instance members should be considered.
d. Static: indicates that the static members should be considered.
e. DeclaredOnly: indicates that the search should not include any inherited members. It should include only those members that are declared at the type's hierarchy level.
The BindingFlags enumeration is used in the Type methods such as GetFields(), GetField(), GetMethods(), GetMethod(), FindMembers(), GetMembers(), GetConstructors(), GetConstructor(). Foe example, an overloaded Type.GetMethods(BindingFlags) looks for the methods of a given type using the specified binding constraints and returns an array of the MethodInfo object where each object represents a method that matches the specified binding constraints.
Get the public, instance methods of the ArrayList class whose names begin with 'To'. The program should return only those methods that are declared for ArrayList i.e. do not include the inherited methods such as Object.ToString().
For Part 5, click this link: Reflection in .NET - Part 5
|posted||Apr 19, 2016|
|active||Apr 20, 2016|
|•||Reflection in .NET - Part 5|
|•||Reflection in .NET - Part 3|
|•||Reflection in .NET - Part 2|
|•||Reflection in .NET - Part 1|
|•||.NET Core project option: "Produce outputs on build"|
|•||Fastest way to serialize a POCO to JSON and deserialize JSON to a POCO|
|•||Serialization in .NET - Part 4|
|•||Reading appsettings.json in a .NET Core console application|
|•||Serialization in .NET - Part 3|
|•||Serialization in .NET - Part 2|