VBCSharp
VBCSharp

Reputation: 101

C# accessing properties of an object from a collection class

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.

Upvotes: 1

Views: 3331

Answers (5)

Stan R.
Stan R.

Reputation: 16065

you need to get the value afterwords. also note that GetValue returns an object, you can then cast it to string or int or whatever type the value you expect is in.

public in SaveCollection(DictionaryBase objCollection, string TableName, string spSave)
{
    PropertyInfo propInfo;

   foreach (DictionaryEntry objClass in objCollection)
      {
           object tempObj = objClass.Value;
           propInfo = tempObj.GetType().GetProperty("TableFieldName");

           object[] obRetVal = new Object[0];
           object value = propInfo.GetValue(tempObj,obRetVal);
       }

}

if you know that TableFieldName will be a string then change this line.

string value = propInfo.GetValue(tempObj,obRetVal) as string;

Upvotes: 1

Steve Wortham
Steve Wortham

Reputation: 22220

Also consider switching to generics if you can. I think you should be able to do something like this:

VB:

Private l As List(Of MyClassName)

C#:

private List<MyClassName> l;

You may not want to use a list, but it's just an example. After that, accessing members of each item in the list can be done through intellisense. Often times generics can be faster too since you don't have to do any casting.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499840

I can't see why you're using a dictionary here - if it's just a collection, why not IEnumerable? Do you actually have a key here that you're storing things by?

DictionaryEntry will never have a TableFieldName property. Are you sure you don't need to take the value (or key) in the DictionaryEntry (using the Value or Key properties).

Do you really need to do this with reflection at all? Can't you use a generic method instead, after defining a common interface that has the TableFieldName property? For example:

public void SaveCollection<T>(IEnumerable<T> collection)
    where T : ITableDescriptor
{
    foreach (T element in collection)
    {
        string tableFieldName = element.TableFieldName;
        // use the table field name here
    }
}

Upvotes: 2

Yuliy
Yuliy

Reputation: 17718

You're looking at the properties of the DictionaryEntry class. Not the class of the value in the particular key-value pair. Consider trying to use objClass.Value.GetType().GetProperty("TableFieldName").

Upvotes: 0

JaredPar
JaredPar

Reputation: 754565

It looks like you are passing a different collection to the VB code and the C# code. My guess is that in the VB code you are passing the values of a dictionary and in C# you are passing the dictionary itself. Try changing the C# line to the following

propInfo = objClass.Value.GetType().GetProperty("TableFieldName");

Upvotes: 3

Related Questions