Solved

Help with c# reflection?

Posted on 2014-02-15
2
235 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 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