Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with c# reflection?

Posted on 2014-02-15
2
Medium Priority
?
238 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
[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 21

Accepted Solution

by:
Craig Wagner earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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