如何从列表中获取值

时间:2016-03-22 13:13:08

标签: c#

问题是:

我有3个班级

public abstract class RelacionamentoComercial
{
    public string cod_usu { get; set; }
    public IList<Endereco> endereco { get; set; }
}

public class Usuario: RelacionamentoComercial
{
    public string senha { get; set; }
    public string cod_est { get; set; }
}

public class Endereco
{
    public string endereco { get; set; }
    public string bairro { get; set; }

}

我正在使用DAO Connections,

public Usuario Save(Usuario usuario)
{var table = abstractDAO.OpenTable(TableName);
        table .Seek(usuario.cod_usu);
        PropertyInfo[] properties = usuario.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propertyValue = property.GetValue(usuario, null);
            if (propertyValue != null)
            {
                if (propertyValue.GetType() == typeof(int))
                    table.PutInteger(property.Name.ToUpper(), Convert.ToInt32(propertyValue));
                else if (propertyValue.GetType() == typeof(double))
                    table.PutDouble(property.Name.ToUpper(), Convert.ToDouble(propertyValue));
                else if (propertyValue.GetType() == typeof(bool))
                    table.PutBoolean(property.Name.ToUpper(), Convert.ToBoolean(propertyValue));
                else if (propertyValue.GetType() == typeof(DateTime))
                    table.PutDate(property.Name.ToUpper(), Convert.ToDateTime(propertyValue));
                else if (propertyValue.GetType() == typeof(string))
                    table.PutString(property.Name.ToUpper(), Convert.ToString(propertyValue));
                else if (propertyValue.GetType().GetGenericTypeDefinition() == typeof(List<>))
                {
                   <<<< ? >>>>
                }

我想获得一个List&lt; Endereco&gt;保存在Clientes,但我不知道该怎么做,我已经尝试过反射

PropertyInfo[] propList = propertyValue.GetType().GetProperties();
                    foreach (PropertyInfo prop in propList)

但没用, 如果有人有更好的想法,请

1 个答案:

答案 0 :(得分:0)

我建议尝试类似的事情:

public static object ConvertList(List<object> value, Type type)
{
    var listType = type.GenericTypeArguments.First();
    return value.Select(item => Convert.ChangeType(item, listType));
}

这应该可以解决问题。

相关问题