Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is reflection in C#, what are the benefit. How to use it to get benifit [closed]

I was reading an article at msdn about reflection but i was not able to understand it even 10% about its benifit, its usage.

Could you please provide some brief overview what is reflection and how can i take benefit from it.

like image 635
Shantanu Gupta Avatar asked Jun 23 '10 12:06

Shantanu Gupta


People also ask

Is there reflection in C?

The Crefl API provides runtime access to reflection metadata for C structure declarations with support for arbitrarily nested combinations of: intrinsic, set, enum, struct, union, field (member), array, constant, variable. The Crefl reflection graph database format for portable reflection metadata.

What is reflection C sharp?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

What is reflection assembly?

Assembly: Mscorlib.dll. Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

Is Gettype reflection?

Or: typeof(MyType); To get the Type , then when one queries the info about the type e.g. getting properties, fields, attributes etc. they are certainly performing reflection.


2 Answers

Reflection allows you to write code that can inspect various aspects about the code itself.

It enables you to do simple things like:

  1. Check the type of an object at runtime (simple calls to typeof() for example)

  2. Inspect the Attributes of an object at runtime to change the behavior of a method (the various serialization methods in .NET)

To much more complicated tasks like:

  1. Loading an assembly at runtime, finding a specific class, determining if it matches a given Interface, and invoking certain members dynamically.

The earlier is much more common usage. The later is helpful to developers working on plug-in architectures for their applications or people who want to swap assemblies at runtime depending on configuration changes.

like image 107
Justin Niessner Avatar answered Oct 15 '22 01:10

Justin Niessner


Reflection is a way for you to programmatically discover Types at runtime. This is very important because .NET languages are strongly-typed. Being able to access that metadata is extremely useful.

A big thing right now (fluent interfaces/adapters) rely heavily on reflection. In particular, static reflection is pretty big. If you want to see specific examples and a good explanation of static reflection, check out:

http://jagregory.com/writings/introduction-to-static-reflection/
http://www.lostechies.com/blogs/gabrielschenker/archive/2009/02/03/dynamic-reflection-versus-static-reflection.aspx

Of course, this a small subset of reflection in general. If you'd like more info about the general use of reflection, check out Apress Pro C# 2008 and the .NET 3.5 Platform, Fourth Edition, Chapter 16. It delves pretty in-depth into the .NET type system and how that is used within libraries and at runtime.

like image 37
Jim Schubert Avatar answered Oct 15 '22 02:10

Jim Schubert