Reflection Explained
The need for Reflection While there are many reasons to use reflection, let me give you a real life example. Say you are building a custom component in .NET that requires a XML file to be used along with your components DLL. You can store the XML file in the /bin directory with your DLL, but how will you access the XML file without knowing the correct path to the /bin directory? I'm sure this is a rhetorical question if you have understand even 10% of what I have wrote so far, but I'll tell you anyways: r e f l e c t i o n. I've created a sample code snippet for you that does just this. Keep reading…
Example usage of Reflection
Here's a neat little trick to find out the calling assemblies path.
// using System.IO; // using System.Reflection; public static string GetApplicationDirectory { get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Replace(@"file:\", ""); } } You can also output the executing assembly information during runtime: <%@ Page language="c#" %> <%@ Import Namespace="System.Reflection"%>
CSharpFriend.com - ASP.NET Sample - reflection
I hope you have understood some basic ideas of what reflection is and what information it can provide you with during runtime.
http://www.c-sharpcorner.com/1/Reflection_in_net.asp
http://weblogs.asp.net/wallym/archive/2004/09/24/234108.aspx
Metadata ( http://odetocode.com/Articles/288.aspx )
A compiler for the common language runtime (CLR) will generate metadata during compilation and store the metadata (in a binary format) directly into assemblies and modules to avoid irregularities. The CLR also allows metadata extensibility, meaning if you want to add custom metadata to a type or an assembly, there are mechanisms for doing so. Metadata in .NET cannot be underestimated. Metadata allows us to write a component in C# and let another application use the metadata from Visual Basic .NET. The metadata description of a type allows the runtime to layout an object in memory, to enforce security and type safety, and ensure version compatibilities.
Metadata & Reflection Reflection is the ability to read metadata at runtime. Using reflection, it is possible to uncover the methods, properties, and events of a type, and to invoke them dynamically. Reflection also allows us to create new types at runtime, but in the upcoming example we will be reading and invoking only. Reflection generally begins with a call to a method present on every object in the .NET framework: GetType. The GetType method is a member of the System.Object class, and the method returns an instance of System.Type. System.Type is the primary gateway to metadata. System.Type is actually derived from another important class for reflection: the MemeberInfo class from the System.Reflection namespace. MemberInfo is a base class for many other classes who describe the properties and methods of an object, including FieldInfo, MethodInfo, ConstructorInfo, ParameterInfo, and EventInfo among others. As you might suspect from thier names, you can use these classes to inspect different aspects of an object at runtime.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|