Solved

Help with c# reflection?

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entity Framework 7 43
Help with C#, MVC, razor. 6 34
Coding C# in Linux 8 69
Problem!!! 8 33
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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