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:
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:
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"):
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.