简体   繁体   English

C#从集合类访问对象的属性

[英]C# accessing properties of an object from a collection class

I am moving from VB to C#. 我正在从VB迁移到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. 我正在使用反射,因为我的方法将不知道它是Contacts类还是Customer类,等等。 Here is my code in VB(watered down) 这是我在VB中的代码(淡化)

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 我在C#中使用objClass.GetType.GetProperty(“ TableFieldName”)行遇到问题

Here is my C# code 这是我的C#代码

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. C#代码不断返回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. 在我的本地窗口中,我可以看到objClass上的类的属性以及属性的值,但是我似乎可以弄清楚如何通过代码访问它。 I used the DictionaryBase because that seems to closely match would I need to do. 我使用了DictionaryBase,因为这似乎与我需要做的非常匹配。 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. 在获取propInfo变量集之后,然后使用字段名,数据类型等设置SQLParameter,然后将值设置为propInfo.value。

Thanks for the help. 谢谢您的帮助。

It looks like you are passing a different collection to the VB code and the C# code. 看来您正在将另一个集合传递给VB代码和C#代码。 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. 我的猜测是,在VB代码中,您正在传递字典的值,而在C#中,您正在传递字典本身。 Try changing the C# line to the following 尝试将C#行更改为以下内容

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

I can't see why you're using a dictionary here - if it's just a collection, why not IEnumerable ? 我看不到您为什么在这里使用字典-如果只是字典,为什么不使用IEnumerable Do you actually have a key here that you're storing things by? 实际上在这里有用来存储事物的密钥吗?

DictionaryEntry will never have a TableFieldName property. DictionaryEntry永远不会具有TableFieldName属性。 Are you sure you don't need to take the value (or key) in the DictionaryEntry (using the Value or Key properties). 您确定不需要在DictionaryEntry中使用 (或键)(使用ValueKey属性)。

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? 在定义具有TableFieldName属性的通用接口之后,不能使用通用方法吗? 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
    }
}

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. 还要注意, GetValue返回一个object ,然后可以将其GetValue为字符串或整数或期望值所在的任何类型。

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. 如果您知道TableFieldName将是一个字符串,则更改此行。

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

You're looking at the properties of the DictionaryEntry class. 您正在查看DictionaryEntry类的属性。 Not the class of the value in the particular key-value pair. 不是特定键值对中值的类别。 Consider trying to use objClass.Value.GetType().GetProperty("TableFieldName") . 考虑尝试使用objClass.Value.GetType().GetProperty("TableFieldName")

Also consider switching to generics if you can. 如果可以的话,还考虑切换到泛型。 I think you should be able to do something like this: 我认为您应该可以执行以下操作:

VB: VB:

Private l As List(Of MyClassName)

C#: 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. 通常,泛型也可以更快,因为您无需进行任何强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM