C# 将容器对象中所有公共对象(string/int/enum)的值写入控制台

C# 将容器对象中所有公共对象(string/int/enum)的值写入控制台,c#,object,return,console-application,object-dumper,C#,Object,Return,Console Application,Object Dumper,我有一个名为Job的对象,其中有字符串、int和枚举作为公共对象。然后将每个作业放入一个队列中,我在流程中遍历该队列 我想做的是,当我对每个作业进行出列()时,我可以遍历每个作业,并将公共对象名称和值写入控制台 我知道了如何将对象名写入控制台,而且我显然可以写入值,但问题是我不知道如何从作业对象获取每个公共字符串/int/enum 我看过 但我不明白我将如何使用这两个公认的答案 以下是我的职业分类代码: class Job { #region Constructor

我有一个名为Job的对象,其中有字符串、int和枚举作为公共对象。然后将每个作业放入一个队列中,我在流程中遍历该队列

我想做的是,当我对每个作业进行出列()时,我可以遍历每个作业,并将公共对象名称和值写入控制台

我知道了如何将对象名写入控制台,而且我显然可以写入值,但问题是我不知道如何从作业对象获取每个公共字符串/int/enum

我看过 但我不明白我将如何使用这两个公认的答案

以下是我的职业分类代码:

    class Job
    {
       #region Constructor
       public Job()
       {
       }
       #endregion

       #region Accessors
       public int var_job { get; set; }
       public jobType var_jobtype { get; set; } //this is an enum
       public string var_jobname { get; set; }
       public string var_content { get; set; }
       public string var_contenticon { get; set; }
       #endregion
    }
    public override string ToString()
    {
        string foo =
            string.Format( "var_job = {1}{0}var_jobType = {2}{0}var_jobname = {3}{0}var_content = {4}{0}var_contenticon = {5}{0}",
                Environment.NewLine,
                this.var_jobname,
                this.jobType,
                this.var_jobname,
                this.var_content,
                this.var_contenticon );

        return foo;
    }
下面是返回变量名称的代码:(from)


想法?

根据Tony Hopkinson的建议,您可以在Job类中添加以下方法重写:

    class Job
    {
       #region Constructor
       public Job()
       {
       }
       #endregion

       #region Accessors
       public int var_job { get; set; }
       public jobType var_jobtype { get; set; } //this is an enum
       public string var_jobname { get; set; }
       public string var_content { get; set; }
       public string var_contenticon { get; set; }
       #endregion
    }
    public override string ToString()
    {
        string foo =
            string.Format( "var_job = {1}{0}var_jobType = {2}{0}var_jobname = {3}{0}var_content = {4}{0}var_contenticon = {5}{0}",
                Environment.NewLine,
                this.var_jobname,
                this.jobType,
                this.var_jobname,
                this.var_content,
                this.var_contenticon );

        return foo;
    }
然后,在排队之前,您可以:

    Console.WriteLine( job1 );

我想你要找的是
PropertyInfo.GetValue
。也许类似的东西会有所帮助(从记忆中,希望它能按原样工作):

如果您倾向于使用对象的字段而不是属性,则也可以使用类似的字段

public static void DumpFields(this Object dumpWhat)
{
    foreach(FieldInfo fld in dumpWhat.GetType().GetFields())
        Console.WriteLine("{0} = {1}", fld.Name, fld.GetValue(dumpWhat, BindingFlags.GetField, null, null, null).ToString());
}
这些文件将转储到控制台,但应该足够直接地将它们更改为写入任何流

更新

如果您开始从未设置的属性获取
NullReferenceException
,而不是将其包装在
try…catch
中,则应针对
PropertyInfo.GetValue
返回的值执行一些主动检查:

public static void DumpProperties(this Object dumpWhat)
{
    foreach(PropertyInfo prop in dumpWhat.GetType().GetProperties())
    {
        string propVal = prop.GetValue(dumpWhat, BindingFlags.GetProperty, null, null, null) as string;

        if (propVal != null)
            Console.WriteLine("{0} = {1}", prop.Name, propVal);
    }
}

使用反射迭代属性,获取值,使用它的tostring()方法。或者重写toString(或另一个方法),该方法从类中构建良好的输出。当然,你必须坚持这一点……我想,通过看下面的例子,我逐渐明白了这一点:当我得到解决方案时,我会发布我的解决方案。为什么你不能重写(或者用不同的名称编写类似的方法)呢在您的
作业
类中?我并不总是初始化作业中的所有值,因此您的第一个DumpProperties方法会抛出一个NullReferenceException,但我只是按如下方式处理:void DumpProperties(Object dumpWhat){foreach(dumpWhat.GetType().GetProperties()中的PropertyInfo prop){try{Console.WriteLine({0}={1}),prop.Name,prop.GetValue(dumpWhat,BindingFlags.GetProperty,null,null,null).ToString();}catch(NullReferenceException){}@cvocvo-一种更干净的处理方法,首先从GetValue获取对象,检查它是否为null,然后将其转换为字符串。之所以更干净,唯一的原因是异常和异常处理代价高昂。我将用一个如何处理这种情况的示例更新我的答案。
public static void DumpProperties(this Object dumpWhat)
{
    foreach(PropertyInfo prop in dumpWhat.GetType().GetProperties())
    {
        string propVal = prop.GetValue(dumpWhat, BindingFlags.GetProperty, null, null, null) as string;

        if (propVal != null)
            Console.WriteLine("{0} = {1}", prop.Name, propVal);
    }
}