如何使用GetType

时间:2016-03-01 17:31:33

标签: c# reflection gettype

我希望获得未知类型的特定对象的Fields / Properties。我的代码主要起作用,但在尝试扩展集合的成员(在本例中为List)时失败。

这是我的代码:

首先是类定义:

 public class Claim
{
    public String icn;
    public String subscriber;
    public DateTime fdos;
    public Double allowed;
    public List<ClaimLine> claimLines;

    public Claim(String icn, String subscriber, DateTime fdos, Double allowed)
    {
        this.icn = icn;
        this.subscriber = subscriber;
        this.fdos = fdos;
        this.allowed = allowed;
        this.claimLines = new List<ClaimLine>();
    }

}

public class ClaimLine
{
    public Int32  lineNumber;
    public String procedure;
    public String diagnosis;
    public Double allowed;


    public ClaimLine(Int32 lineNumber, String procedure, String diagnosis, Double allowed)
    {
        this.lineNumber = lineNumber;
        this.procedure = procedure;
        this.diagnosis = diagnosis;

        this.allowed = allowed;

    }

}

现在构建和对象的代码并测试解析器(注意:我的目标不是构建xml,但它可以很好地证明概念,因为我可以轻松地打印或导出它)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace TypeParser
{
    class Program
    {
        static void Main(string[] args)
        {
            Claim claim = new Claim("12345678901234", "1234567890000",    DateTime.Now, 0.0);  //build main object
            claim.claimLines.Add(new ClaimLine(1, "PROC1", "DIAG1", 10.55)); //add a couple of items to the list
            claim.claimLines.Add(new ClaimLine(2, "PROC2", "DIAG2", 55.10));



            String parsedType = parseType(claim, "");


            Console.WriteLine(parsedType);
            Console.ReadLine();
        }

        static String parseType(Object obj,String parsedType)
        {
            parsedType += "<" + obj.GetType().Name + ">\n";
            foreach (FieldInfo fi in obj.GetType().GetFields())
            {
                if (fi.GetValue(obj) != null)
                {
                    if (fi.FieldType.Name == "String" || fi.FieldType.IsPrimitive || fi.FieldType.Name == "DateTime")
                    {
                        parsedType += "<" + fi.Name + " type=" + fi.FieldType.Name + ">" + fi.GetValue(obj) + "</" + fi.Name + ">\n";
                    }
                    else
                    {
                        parsedType += parseType(fi.GetValue(obj), parsedType); //Property is not primitive so recurse
                    }
                }
            }
            foreach (PropertyInfo pi in obj.GetType().GetProperties())
            {

                ParameterInfo[] paramInfo = pi.GetIndexParameters();

                if (paramInfo.Length > 0)
                {
                   //some code to implement processig list
                }
                else
                {

                    if (pi.PropertyType.Name == "String" || pi.PropertyType.IsPrimitive || pi.PropertyType.Name == "DateTime")
                    {
                            parsedType += "<" + pi.Name + " type=" + pi.PropertyType.Name + ">" + pi.GetValue(obj, null) + "</" + pi.Name + ">\n";
                    }
                    else
                    {
                        //Property is not primitive so recurse
                        parsedType += parseType(pi.GetValue(obj, null), parsedType);
                    }

                }
            }


            parsedType += "</" + obj.GetType().Name + ">\n";

            return parsedType;
        }
    }
}

它是标记//部分代码以实现processig列表的部分 我遇到了困难。我不知道如何获得价值。

1 个答案:

答案 0 :(得分:1)

即使您正在寻找代码来替换//some code to implement processig list注释以及执行此操作的过程,我采用了不同的方法。问题是在递归时处理List<ClaimLine>时,它会处理List对象并尝试获取该属性,这不是您想要的。您希望单独处理列表中的每个ClaimLine对象,并将其传递给parseType,而不是整个List对象。

为了完成这项工作,我更换了以下一行:

parsedType += parseType(fi.GetValue(obj), parsedType); //Property is not primitive so recurse

以下内容:

//Property is not primitive so recurse
                    if (fi.GetValue(obj) is IEnumerable)
                    {
                        //Object is IEnumerable, process each object separately
                        foreach (var o in (IEnumerable)fi.GetValue(obj))
                        {
                            parsedType += parseType(o, parsedType); 
                        }
                    }
                    else
                    {
                        //Object is not IEnumerable, process it
                        parsedType += parseType(fi.GetValue(obj), parsedType);
                    }

这将检查对象是否IEnumerable,(List<ClaimLine>是),遍历单个对象。