?
Solved

reflection explaination

Posted on 2010-01-12
2
Medium Priority
?
235 Views
Last Modified: 2012-05-08
Hi,

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?
0
Comment
Question by:dchau12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick 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
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
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
            System.Diagnostics.Debug.WriteLine("Constructors:");
            foreach (ConstructorInfo cf in info)
            {
                System.Diagnostics.Debug.WriteLine(cf);
            }

            System.Diagnostics.Debug.WriteLine("");
            // get all the methods
            System.Diagnostics.Debug.WriteLine("Methods:");
            foreach (MethodInfo mf in methods)
            {
                System.Diagnostics.Debug.WriteLine(mf);
            }

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


        }

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

771 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