reflection explaination

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?
dchau12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Meir RivkinFull stack Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anarki_jimbelSenior DeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.