Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Guys/Gals,

Firstly, if I search for my question in Google I get ALOT of result.
I've read a few of them, and it might just be their explanations but I don't understand if what they are doing will give me what I need.

Basically I have a class which looks a bit like this:

C#
public class Location {
   public string Area {get;set;}
   public string Depot {get;set;}
}


I am reading an Excel into a dataset and then processing the rows in one of the tables.
I have a list of headers which are stored in an object that looks a bit like this:

C#
public class HeaderItem {
   public string HeaderText {get;set;}
   public string PropertyName {get;set;}
}


What I'm trying to do is something like this (this is a mix of code and "sudo"):

C#
foreach(DataRow row in myDataTable)
{
   var loc = new Location();
   loc.Area = row[myHeaders.FirstWhere(h=>h.PropertyName == ##Get "Area" as string##).HeaderText].ToString();
}   


------------------------------------
Edit based on first comment:

I would like to put something like:

loc.Area = row[myHeaders.FirstWhere(h=>h.PropertyName == "Area").HeaderText].ToString();

But rather than have a literal string in the comparison use the property name.

-------------------------------------



The examples I've seen have generally refereed to using using
System.Reflection.MethodBase.GetCurrentMethod()
Which I would imagine in this case would return the name of the wrapped method (in this case "ReadLocation").

Is there a way I can do what I'm after, or a better way than my current approach?

For reference these are two questions out of the many I've read already:
http://stackoverflow.com/questions/15333249/how-to-get-name-of-current-property[^]
http://stackoverflow.com/questions/1206023/how-to-get-current-property-name-via-reflection[^]

I did wonder about using this principle:
http://www.csharp-examples.net/reflection-property-names/[^]

But I could figure the logic of how to go about it, so if anyone has any ideas I'd appreciate it.
Posted
Updated 5-Mar-15 2:00am
v2
Comments
Kuthuparakkal 5-Mar-15 7:55am    
I didnt quite understand the objective. Could you explain it in simple English rather than pseudo code ?
Pheonyx 5-Mar-15 8:00am    
I've edited the question, hope that additional helps.
Kuthuparakkal 5-Mar-15 8:51am    
I posted a solution, please have a look.
George Jonsson 5-Mar-15 8:09am    
If I understand you correctly, you want to get the string "Area" from code instead of manually hard code it?
In that case Reflection is your friend. Look for Type.GetProperties and the likes.

This will be trivial in C# 6:
C#
loc.Area = row[myHeaders.First(h => h.PropertyName == nameof(loc.Area)).HeaderText].ToString();

For earlier versions, something like this[^] will work, albeit with a slight performance penalty:
C#
loc.Area = row[myHeaders.First(h => h.PropertyName == default(Location).GetMemberName(x => x.Area)).HeaderText].ToString();


NB: You don't need a custom FirstWhere extension method; the built-in First method[^] has an overload which accepts a filter predicate.
 
Share this answer
 
Comments
BillWoodruff 5-Mar-15 8:58am    
+5 perfect
Pheonyx 5-Mar-15 9:11am    
Thank you, this has really helped me. I knew I was missing something and now I look at your code it's obvious what I was missing. Much appreciated +5
Please try this:
C#
foreach(DataRow row in myDataTable.Rows)
{
    var loc = new Location();
    var properties = loc.GetType().GetProperties();

    foreach (PropertyInfo pinfo in properties)
    {
        HeaderItem tempheader = myHeaders.FirstOrDefault(h => h.PropertyName == pinfo.Name);

        if (tempheader != null)
        {
            object value = row[tempheader.HeaderText].ToString();
            pinfo.SetValue(loc, value, null);
        }
    }

}
 
Share this answer
 
Comments
Pheonyx 5-Mar-15 9:12am    
This was really helpful, and it looks like it will solve my problem in a more re-usable way than what I was doing. I've accepted bother as answers as they help with both aspects of what I was trying to do. Much appreciated. +5
Kuthuparakkal 5-Mar-15 9:43am    
you are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900