Solved

Help with c# reflection?

Posted on 2014-02-15
2
224 Views
Last Modified: 2014-02-16
I am trying to write a WPF custom control, and am trying to refactor it so that any List<T> can be converted into a List<CustomType>.

The CustomType has an Id property that I want to be assigned with the value of the Id property from an instance of the source T.

But my problem is how to map the name of the Id property on the source to the Id property on the CustomType.

A pseudocode method follows:

private void UsePropertiesOfInputCollectionInCollectionOfAnotherClass(IEnumerable<object> inputCollection , string nameOfTheMemberThatHoldsTheIdValueOfAnInputObject, Type typeOfTheObjectsInTheCollection)
        {
            foreach (var inputObject in inputCollection)
            {

                //cast the input object to the typeOfTheObjectsInTheCollection


                //interogate the member on the (cast)inputObject that is named: nameOfTheMemberThatHoldsTheIdValueOfAnInputObject


                //Create a new instance of the AnotherClass


                //assign the value of the interogated member to a property called AnotherClass.ClassID



            }
        }


Can you help?
0
Comment
Question by:quentinA
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39862853
You should make your method generic, it will save you time and trouble.

I threw this together in about two minutes. It doesn't do any error checking (e.g. what if the property doesn't exist on the input object, what if the datatypes are incompatible, etc), but it's enough to get you started.

private void UsePropertiesOfInputCollectionInCollectionOfAnotherClass<T>(
    IEnumerable<T> inputCollection, string nameOfIdProperty )
{
    foreach( var inputObject in inputCollection )
    {
        var propInfo = typeof( T ).GetProperty( nameOfIdProperty );

        var anotherClass = new AnotherClass();

        anotherClass.ClassId = (int)propInfo.GetValue( inputObject );
    }
}

Open in new window


P.S. This type of question, when you even have comments of what needs to be done, often smells like a school assignment. I hope that isn't the case here.
0
 

Author Closing Comment

by:quentinA
ID: 39862968
Thanks very much.
It isn't a school assignment (although I often feel as though I am at school, because i'm teaching myself).

I had done following (which works), I'll try yours too.
           var starterObjectType = starterCollection.First().GetType();

            foreach (var starterObject in starterCollection)
            {
                //cast the input object to the typeOfTheObjectsInTheCollection
                MethodInfo castMethod = this.GetType().GetMethod("Cast").MakeGenericMethod(starterObjectType);
                object castedObject = castMethod.Invoke(null, new object[] { starterObject });

                //interogate the member on the (cast)inputObject that is named: nameOfTheMemberThatHoldsTheIdValueOfAnInputObject
                var idResult = GetPropValue(castedObject, nameOfTheMemberThatHoldsTheIdValueOfAnInputObject).ToString();
                var iiid = Guid.Parse(idResult);

                //assign the value of the interogated member to a property called AnotherClass.ClassID
                RecordStarterProperties.Add(new StarterProperty(iiid, textOfTheMember, starterObject));
            }
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now