9

What is reflection in C#? Where do we use this concept in our applications?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HITESH
  • 121
  • 2
  • 3
  • 6
  • 1
    The first hit is already not too bad: http://www.google.com/search?q=what+is+reflection+in+c%23+%3F+where+we+use+this+concept+in+our+application%3F – Dirk Vollmar Apr 26 '10 at 10:23
  • 2
    Am I dreaming or the title of the question is longer than the question itself ? – ereOn Apr 26 '10 at 10:27
  • 7
    @0xA3: I'm pretty sure our knee-jerk response to someone asking an introductory question on StackOverflow isn't supposed to be "go Google it yourself" or "RTFM". – Scott Whitlock Apr 26 '10 at 10:27
  • @Scott, Plus, as of this moment, the first hit on the link 0xA3 posted refers back to this question. So I guess this is a case of GNSO. *8') – Mark Booth Apr 26 '10 at 12:05
  • Combined duplicate of http://stackoverflow.com/questions/1859902/in-3-minutes-what-is-reflection and http://stackoverflow.com/questions/1897712/reflection-what-can-we-achieve-using-it ? – Jørn Schou-Rode Apr 26 '10 at 20:25

4 Answers4

16

Reflection provides objects (of type Type) that encapsulate 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. If you are using attributes in your code, reflection enables you to access them...

For reference, MSDN article on reflection and The Code Project has reflection pretty well explained..

For example, have a look at C# Reflection Examples.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ACP
  • 34,682
  • 100
  • 231
  • 371
5

From the documentation:

Reflection provides objects (of type Type) that encapsulate 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. If you are using attributes in your code, Reflection enables you to access them. For more information, see Attributes.

Wikipedia says this:

In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior. The programming paradigm driven by reflection is called reflective programming. It is a particular kind of metaprogramming.

For example, if you want to programmatically display all the methods of a class, you could do it like so:

using System;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            var t = typeof(MyClass);

            foreach (var m in t.GetMethods())
            {
                Console.WriteLine(m.Name);
            }
            Console.ReadLine();
        }
    }


    public class MyClass
    {
        public int Add(int x, int y)
        {
            return x + y;
        }

        public int Subtract(int x, int y)
        {
            return x - y;
        }
    }
}
Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
3

One use of reflection you will find in frameworks: to perform a particular function (in that framework) some class is used. But the exact class isn't known at compile time, instead it is configured in some text file, as the classname (usually including it's assembly). Using reflection you can take this string and create an instance of that particular class.

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
0

You have answers above of what reflection is, some cases where I've used it in the past:

  • A generic method that takes in a list of properties and copies those properties in one object to another
  • A method that uses the Description attribute to get text for enum, similar to this

Also see this article for examples of reflection, further links at bottom of the page.

Fermin
  • 34,961
  • 21
  • 83
  • 129
  • thanks for giving me such link,but 1 question is that when we add as namespace in simple .cs page or some other page ? plz tell me that? – HITESH Apr 26 '10 at 10:43
  • Sorry, I'm not sure what you mean. The namespace for reflection is System.Reflection. Is this what you mean? – Fermin Apr 26 '10 at 12:37