Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


reflection explaination

Posted on 2010-01-12
Medium Priority
Last Modified: 2012-05-08

I was trying to explain reflection to a colleauge, and I'm not sure I did such a good job.  Here is the code example:

            MethodInfo addMethod = this.ObjectTargetSource.GetType().GetMethod("Add");
            MethodInfo containsMethod = this.ObjectTargetSource.GetType().GetMethod("Contains");

            if (addMethod != null && containsMethod != null)
                foreach (var item in dg.SelectedItems)
                    bool contains = (bool)containsMethod.Invoke(ObjectTargetSource, new object[] { item });

                    if (!contains)
                        addMethod.Invoke(this.ObjectTargetSource, new object[] { item });
I told him that ObjectTargetSource is of type IEnumerable, but the original type of the object is an ObservableCollection.  Because an ObservableCollection imlements the interface of IEnumerable, it knows it has the "Add" and "Contains" method, and can access it through reflection.

This is probably a messed up explaination.  Can someone do a better job of explaining this?
Question by:dchau12
LVL 42

Accepted Solution

Meir Rivkin earned 1000 total points
ID: 26294377
u can start with give him a brief overview of reflection, which is merely a method to strip down assemblies, modules, and types.
using reflection provides u the ability to "investigate" any CLR type in terms of getting its methods, properties, attributes, as well as describing the object properties (BaseType, IsClass, IsInterface,IsPublic,IsStatic etc).
naturally, when object inherit or implement another object, the reflection provides u an API to detect which is the base type.

u can give him a simpler example, by defining a simple type and walk through its type's properties.
for instance:
 int i = 0;
Type t = i.GetType();

debug this code and in Watch window u can expand all the type properties.
then u can move to more complex type like object, collection etc.
good luck
LVL 30

Assisted Solution

anarki_jimbel earned 1000 total points
ID: 26300491
Basicly reflection is the feature in .Net, which enables to get some information about object in runtime. That information contains data of the class including names of the methods that are inside the class and constructors of that object, property names. It has much more like creating and running methods on a fly but don't confuse beginners :).

Try the example below:
   public class MyTestClass
        public MyTestClass()
           counter = 1;

        public MyTestClass(int c)
           counter = c;

        private int counter;

        public int Inc()
           return counter++;
        public int Dec()
           return counter--;

        // public property
        public int Counter
            get { return counter; }


        private void button2_Click(object sender, EventArgs e)
            //DateTime dt = DateTime.Now;

            //MessageBox.Show(dt.ToString("d MMM yyyy, dddd, HH:mm"));

            MyTestClass testObject = new MyTestClass(15);
            Type objectType = testObject.GetType();

            ConstructorInfo[] info = objectType.GetConstructors();
            MethodInfo[] methods = objectType.GetMethods();
            PropertyInfo[] properties = objectType.GetProperties();

            // get all the constructors
            foreach (ConstructorInfo cf in info)

            // get all the methods
            foreach (MethodInfo mf in methods)

            // get all the methods
            foreach (PropertyInfo  pf in properties)
                System.Diagnostics.Debug.WriteLine("  - Property Name: " + pf.Name + "; property type: " + pf.PropertyType);


Open in new window


Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question