解析属性信息C#

时间:2013-04-18 13:50:50

标签: c# asp.net-mvc parsing

伙计我有以下循环,如果是一个List我要再次循环,直到这里我没事,但在这个另一个循环中我遇到了一个问题,因为我要循环不在“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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

尝试

objeto.GetValue(processo, null)

而不是

objeto.GetValue(objeto, null)