I am moving from VB to C#. I am trying to loop through a collection class which is a collection of data classes but I can't seem to get the actual values out of the data class properties(find the correct code to do so). I have a method that loops through the collection class(Contacts) and saves each record(Contact). I am using reflection because my method will not know if it is Contacts class or a Customer class and so forth. Here is my code in VB(watered down)
Public Function SaveCollection(ByVal objCollection as Object, ByVal TableName as string, ByVal spSave as string)
Dim objClass as Object
Dim propInfo as PropertyInfo
For Each objClass in objCollection
propInfo = objClass.GetType.GetProperty("TableFieldName")
Next
End Function
I am having problems in C# with the objClass.GetType.GetProperty("TableFieldName") line
Here is my C# code
public in SaveCollection(DictionaryBase objCollection, string TableName, string spSave)
{
PropertyInfo propInfo;
foreach (DictionaryEntry objClass in objCollection)
{
propInfo = objClass.GetType().GetProperty("TableFieldName")
}
}
The C# code keeps returning null. In my locals window I can see the proprties on the class on objClass and the value of the propery but I can seem to figure out how to access it through code. I used the DictionaryBase because that seems to closely match would I need to do. My data class(Contact) has a bunch or properties that match the field names in the database of the Contact Table. After I get the propInfo variable set I then set up my SQLParameter with the fieldname, datatype etc and then set the value to the propInfo.value.
Thanks for the help.