解析c#属性信息

本文关键字:信息 属性 解析 | 更新日期: 2023-09-27 18:09:57

伙计们,我有以下循环,如果是一个列表,我必须再次循环,直到这里我是好的,但在这另一个循环我遇到了一个问题,因为我必须循环不在"propretyinfo",我必须在这个propretyinfo的对象循环,并得到这个值,我已经尝试了一个艰难的解析在这里:

(List<ItemDeMescla>)objeto.GetValue(objeto, null)

但是它抛出了一个异常,知道如何解析它和工作吗?

foreach (PropertyInfo objeto in processo.GetType().GetProperties())
{
    corpoEmail += CriarLinhaEmail(objeto.Name, Convert.ToString(objeto.GetValue(processo, null)), false);
    if (objeto.PropertyType.IsGenericType && (objeto.PropertyType.GetGenericTypeDefinition() == typeof(System.Collections.Generic.List<>)))
    {
        List<ItemDeMescla> itensMescla = (List<ItemDeMescla>)objeto.GetValue(objeto, null);
        foreach (ItemDeMescla item in itensMescla)
        {
            tabelasAux.Add(CriarTabelaInternaEmail<ItemDeMescla>(item, objeto.Name));
        }
    }
}

错误堆栈:

    at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at SACG.Services.Integracao.Integracao.Utils.MontarCorpoEmail[T](T processo, String mensagem) in c:'Projetos'Cotrijal'Branch'1.8.0.0'Cotrijal'Servicos'SACG.Services.Integracao'Integracao'Utils.cs:line 176
   at SACG.Services.Integracao.Integracao.Utils.EnviarEmail[T](T processo, Int32 codigoDaUnidade, String mensagem, String assunto) in c:'Projetos'Cotrijal'Branch'1.8.0.0'Cotrijal'Servicos'SACG.Services.Integracao'Integracao'Utils.cs:line 114
   at SACG.Services.Integracao.Integracao.IntegracaoDaExpedicao.SalvarExpedicao(ExpedicaoGraos expedicaoDto, Usuario usuarioLogado) in c:'Projetos'Cotrijal'Branch'1.8.0.0'Cotrijal'Servicos'SACG.Services.Integracao'Integracao'IntegracaoDaExpedicao.cs:line 111
   at SACG.Services.Integracao.ExpedicaoDeGraos.RegistrarExpedicao(ExpedicaoGraos expedicao) in c:'Projetos'Cotrijal'Branch'1.8.0.0'Cotrijal'Servicos'SACG.Services.Integracao'ExpedicaoDeGraos.asmx.cs:line 104

解析c#属性信息

尝试获取一个对象的值,而不是PropertyInfo:

(List<ItemDeMescla>)objeto.GetValue(processo, null)

Try

objeto.GetValue(processo, null)

代替

objeto.GetValue(objeto, null)