Sunday, March 13, 2011

.NET Reflection, Assemblies, Value types and Reference types

Since I am less busy today, thought to write another post about one of the most powerful and pretty interesting mechanisms in .NET framework which is .NET Reflection.

.NET Reflection allows the developer to observe and modify the program's structure at run time. The most interesting thing in Reflection is we can get all the information about loaded assemblies and types at Run Time.

When talking about assemblies, using assemblies we can obtain all the information about the fully functionality of a any .NET application. Actually .NET Reflection provides a developer a API (Application Programming Interface) to take a closer look at assemblies. Other than that, reflection API also allows developer to dynamically create assembly in memory and use it in the program code. The namespace for using .NET Reflection is System.Reflection.

When talking about Types, Types can be either Value types or Reference types. If you are wondering what are Value types and Reference types I'll give you all a brief introduction.
  • Value Types
    • Enumerators, Structs, and Primitives(except 'string').
    • Value types are stored in Stack
    • Value types cannot be null.
    • When passing a variable that is a value type, it passes that variable's value and not a reference to its underlying object.
  • Reference Types
    • Classes, Arrays, Delegates, Interfaces and string are reference types.
    • Reference types are stored in Heap.

There are a lot of things which can achieved using Reflection. I'll share some of the important ones.
  1. You can use reflection to create type instances at run time, invoke them and most importantly access them is run time. 
  2. Using reflection, you can find out the details of an object's methods in terms of its access modifier ( private, public etc.)
  3. You can discover the name and types of parameters in a methods signature.
For more details information about .NET Reflection visit MSDN, 

And that's how I understands .NET Reflection. Feel free to correct me or give me your feedback.

Happy Coding.

Regards,
Jaliya