Skip to main content

Reflection in .NET




Namespace: System.Reflection

Reflection is used to interrogate, invoke and generate .Net code on the fly. Code in the CLR is packaged in an assembly, which is a logical container for different type of CLR data. It includes:

Assembly Metadata
It provides information about the Name, version, Strong name, culture information. It’s also known as Manifest information.

Type Metadata
Type information, namespace, name, exposed members, properties, & methods.

Code (Intermediate Language Code)
Code is the part that is compiled and executed.

Resources
String, images, or files that are used from the code are the part of resources.

MultiFile Assembly:

            VS does not support multifile assemblies, one can use Command Line or MSBuild to create multifile assembly.

 



Assembly:
                It defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

Assembly class:
CreateInstance
Overloaded. Locates a type from this assembly and creates an instance of it using the system activator.
CreateQualifiedName
Creates the name of a type qualified by the display name of its assembly.
Equals 
Overloaded. Determines whether two Object instances are equal. (Inherited from Object.)
GetAssembly
Gets the currently loaded assembly in which the specified class is defined.
[S] GetCallingAssembly
Returns the Assembly of the method that invoked the currently executing method.
GetCustomAttributes
Overloaded. Gets the custom attributes for this assembly.
GetEntryAssembly
Gets the process executable in the default application domain. In other application domains, this is the first executable that was executed by AppDomain.ExecuteAssembly.
[S] GetExecutingAssembly
Gets the assembly that contains the code that is currently executing.
GetExportedTypes
Gets the exported types defined in this assembly that are visible outside the assembly.
GetFile
Gets a FileStream for the specified file in the file table of the manifest of this assembly.
GetFiles
Overloaded. Gets the files in the file table of an assembly manifest.
GetHashCode 
Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.)
GetLoadedModules
Overloaded. Gets all the loaded modules that are part of this assembly.
GetManifestResourceInfo
Returns information about how the given resource has been persisted.
GetManifestResourceNames
Returns the names of all the resources in this assembly.
GetManifestResourceStream
Overloaded. Loads the specified manifest resource from this assembly.
GetModule
Gets the specified module in this assembly.
GetModules
Overloaded. Gets all the modules that are part of this assembly.
GetName
Overloaded. Gets an AssemblyName for this assembly.
GetObjectData
Gets serialization information with all of the data needed to reinstantiate this assembly.
GetReferencedAssemblies
Gets the AssemblyName objects for all the assemblies referenced by this assembly.
GetSatelliteAssembly
Overloaded. Gets the satellite assembly.
GetType
Overloaded. Gets the Type object that represents the specified type.
GetTypes
Gets the types defined in this assembly.
IsDefined
Indicates whether a custom attribute identified by the specified Type is defined.
[S]Load
Overloaded. Loads an assembly.
LoadFile
Overloaded. Loads the contents of an assembly file.
[S] LoadFrom
Overloaded. Loads an assembly.
LoadModule
Overloaded. Loads the module internal to this assembly.
LoadWithPartialName
Overloaded. Loads an assembly from the application directory or from the global assembly cache using a partial name.
[S] ReferenceEquals 
Determines whether the specified Object instances are the same instance. (Inherited from Object.)
ReflectionOnlyLoad
Overloaded. Loads an assembly into the reflection-only context, where it can be examined but not executed.
ReflectionOnlyLoadFrom
Loads an assembly into the reflection-only context, given its path.
ToString
Overridden. Returns the full name of the assembly, also known as the display name.
[S] Static Method

Module: A module is a portable executable file of type.dll or .exe consisting of one or more classes and interfaces. There may be multiple namespaces contained in a single module, and a namespace may span multiple modules.

One or more modules deployed as a unit compose an assembly.

Public Properties:
Name
Description
Assembly
Gets the appropriate Assembly for this instance of Module.
FullyQualifiedName
Gets a string representing the fully qualified name and path to this module.
MDStreamVersion
Gets the metadata stream version.
MetadataToken
Gets a token that identifies the module in metadata.
ModuleHandle
Gets a handle for the module.
ModuleVersionId
Gets a universally unique identifier (UUID) that can be used to distinguish between two versions of a module.
Name
Gets a String representing the name of the module with the path removed.

Public Methods:
Name
Description
Equals 
Overloaded. Determines whether two Object instances are equal. (Inherited from Object.)
FindTypes
Returns an array of classes accepted by the given filter and filter criteria.
GetCustomAttributes
Overloaded. Returns custom attributes.
GetField
Overloaded. Returns a specified field.
GetFields
Overloaded. Returns the global fields defined on the module.
GetHashCode 
Serves as a hash function for a particular type. GetHashCode is suitable for use in hashing algorithms and data structures like a hash table. (Inherited from Object.)










GetType
Overloaded. Returns the specified type.
GetTypes
Returns all the types defined within this module.
IsDefined
Determines if the specified attributeType is defined on this module.
IsResource
Gets a value indicating whether the object is a resource.
ReferenceEquals 
Determines whether the specified Object instances are the same instance. (Inherited from Object.)
ResolveField
Overloaded. Returns the field identified by a metadata token.
ResolveMember
Overloaded. Returns the type or member identified by a metadata token.
ResolveMethod
Overloaded. Returns the method identified by a metadata token.




Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Concept of App Domain in .Net

Creating Application Domains: Application domain is just like process, provides separate memory space, and isolates from other code. But it’s quite light weight. It also provides the following advantages: 1-       Reliability : If a domain crashes, it can be unloaded. Hence doesn’t affect the other assemblies. 2-       Efficiency : Loading all assemblies in one domain can be cumbersome and can make the process heavy but Appdomains are efficient in this manner. Important properties of AppDomain: ApplicationIdentity , ApplicationTrust , BaseDirectory , CurrentDomain , DomainManager , DomainDirectory , Evidence , FriendlyName , ID , RelativeSearchPath , SetupInformation , ShadowCopyFiles . Important methods of AppDomain: ApplyPolicy , CreateCOMInstanceFrom , CreateDomain , CreateInstance (Assembly). To create an AppDomain: AppDomain adomain = AppDomain.CreateDomain(“D”); To execute an assembly:...

Asynchronous Execution in ASP.NET

Asynchronous Execution: Two ways either implement IHTTPAsyncHandler interface or in ASP.NET 2.0 set <%@ Page Async=”true” %>. The second option implements IHTTPAsyncHandler interface automatically for the page when parsed or compiled. AddOnPreRenderCompleteAsync ( new BeginEventHandler(BeginTask), new EndEventHandler(EndTask)); AddOnPreRenderCompleteAsync() shoud be called in Page_load. The BeginEventHandler and EndEventHandler are delegates defined as follows: IAsyncResult BeginEventHandler( object sender, EventArgs e, AsyncCallback cb, object state) void EndEventHandler( IAsyncResult ar) AsyncProcess starts and completes between PreRender and PreRenderComplete. Other way to perform Async Task is using AsyncPageTask structure. It also allows multiple tasks to execute simultaneously. void Page_Load (object sender, EventArgs e) { PageAsyncTask task = new PageAsyncTask( new BeginEventHandler(BeginTask), new EndEventH...