2012-01-03 41 views
2

我有一个名为Job的对象,它内部有字符串,整数和枚举作为公共对象。然后将每个作业放入一个队列中,并遍历流程中的队列。将容器对象中的所有公共对象(字符串/整数/枚举)的值写入控制台

我想要做的是,当我出列每个作业时,我可以一般地遍历每个作业并将公共对象名称和值写入控制台。

我想出了如何编写对象名称到控制台,我可以明显写入值,但问题是我不知道如何从Job对象获取每个公共字符串/ int /枚举。

我已经看了 C# object dumper ​​ How to select all the values of an object's property on a list of typed objects in .Net with C# 但不明白我怎么会请使用接受答案的出现。

下面的代码到我的作业类:

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 
    } 

这里是一个的返回变量的名称代码:(从https://stackoverflow.com/a/2664690/559988

GetName(new {Job.var_content}) //how I call it 
static string GetName<T>(T item) where T : class 
{ 
    return typeof(T).GetProperties()[0].Name; 
} 

理想我有一个输出到控制台这样:

Queuing Jobs Now 
-------------------- 
var_job = its value 
var_jobtype = its value 
var_jobname = its value 
var_content = its value 
var_contenticon = its value 

想法?

+0

使用反射遍历属性,获取值,使用它的tostring()方法。或者重写toString(或另一种方法),从类中构建一个很好的输出。当然,你必须保持这一点...... – 2012-01-03 17:34:13

+0

我想我逐渐通过在这里看例子:http://msdn.microsoft.com/en-us/library/aa332475(v=vs.71 ).aspx当我得到它时,我会发布我的解决方案。 – cvocvo 2012-01-03 17:50:15

+2

为什么不能在你的''ToString'(http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx)(或者用不同的名字写一个类似的方法)工作类? – 2012-01-03 18:40:57

回答

3

我认为你有”重新寻找是PropertyInfo.GetValue。或许,这样的事情将有助于(从内存中,所以希望它会工作为是):

public static void DumpProperties(this Object dumpWhat) 
{ 
    foreach(PropertyInfo prop in dumpWhat.GetType().GetProperties()) 
     Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(dumpWhat, BindingFlags.GetProperty, null, null, null).ToString()); 
} 

您还可以,如果你倾向于与对象的字段类似使用这些替代性。

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); 
    } 
} 
+0

我并不总是初始化作业中的所有值,所以你的第一个DumpProperties方法抛出一个NullReferenceException,但我只是处理,如下所示:void DumpProperties(Object dumpWhat) foreach(PropertyInfo prop in dumpWhat.GetType()。 GetProperties()) { try Console.WriteLine(“{0} = {1}”,prop.Name,prop.GetValue(dumpWhat,BindingFlags.GetProperty,null,null,null).ToString()) ; } 赶上(NullReferenceException异常) {} } } – cvocvo 2012-01-03 19:06:59

+0

@cvocvo - 处理一个清洁的方式是首先得到的GetValue对象,检查它是否是空的,如果它不然后将其转换到一个字符串。这是更清洁的唯一原因是因为异常和异常处理是昂贵的。我将以我如何处理这种情况为例来更新我的答案。 – 2012-01-03 19:14:03

1

每托尼·霍普金森的建议,你可以在下面的方法重写添加到您的作业类:

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); 
相关问题