Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何输出类(struct)中的所有项_C#_Class_Struct_Output - Fatal编程技术网

C#如何输出类(struct)中的所有项

C#如何输出类(struct)中的所有项,c#,class,struct,output,C#,Class,Struct,Output,我的课基本上只有一排桌子。 此行包含许多列 出于测试目的,我需要输出得到的读数 所以我需要输出行中的所有列 这个班就像 public class tableRow { public tableRow() {} public string id public string name public string reg public string data1 .... .... .... &

我的课基本上只有一排桌子。 此行包含许多列

出于测试目的,我需要输出得到的读数

所以我需要输出行中的所有列

这个班就像

    public class tableRow
    {
        public tableRow()
        {}
    public string id
    public string name
    public string reg
    public string data1
    ....
    ....
    ....
   <lot more columns>

}  
公共类表格行
{
公共表格行()
{}
公共字符串id
公共字符串名
公共字符串注册表
公共字符串数据1
....
....
....
}  
然后我需要这样写:

Console.WriteLine("id: " + tableRow.id);
Console.WriteLine("name: " + tableRow.name);
Console.WriteLine("reg: " + tableRow.reg);
Console.WriteLine("data1: " + tableRow.data1);
...
...
...
<lot more Console.WriteLine>
Console.WriteLine(“id:+tableRow.id”);
Console.WriteLine(“名称:”+tableRow.name);
Console.WriteLine(“reg:+tableRow.reg”);
Console.WriteLine(“data1:+tableRow.data1”);
...
...
...
所以我想知道,有没有一种简单的方法可以在没有这么多console.writeLine的情况下获得所有这些输出


谢谢你可以用反射来做。。。给我一分钟,我会发布一些代码

一些事情:

PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |
                                              BindingFlags.Static);
// sort properties by name
Array.Sort(propertyInfos,
        delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
        { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });

// write property names
foreach (PropertyInfo propertyInfo in propertyInfos)
{
  Console.WriteLine(propertyInfo.Name);
}
您可以添加一个检查以查看它是一个字符串,也可以添加一些输出,但这就是它的git


如果设置了字符串数组,则从

中选择源

var dataFields = new string[] { "id", "name", ...
您可以执行一个foreach循环来填充表,并使用相同的数组引用表数据,从而允许您执行另一个foreach循环来执行Console.WriteLine调用


如果需要,每个表行可以只是一个字典,数据字段作为键,数据作为值。然后,您可以在字典中循环

您可以将tableRow序列化为JSON,所有列都将被打印出来。例如,使用(可从NuGet获得):

输出:

{
  "id": "42",
  "name": "Bob",
  "reg": "Foo",
  "data1": null
}
test: 
test2: 
test3: 0
test4: 0

test: haha
test2: 
test3: 0
test4: 0

我使用这种方法进行日志记录(显示对象状态)——有扩展很好

public static string ToJson<T>(this T obj)
{ 
    return JsonConvert.SerializeObject(tr, Formatting.Indented);
}

这应该适用于具有自定义类型描述符的类和类型:

private static void Dump(object o)
{
    if (o == null)
    {
        Console.WriteLine("<null>");
        return;
    }

    var type = o.GetType();
    var properties = TypeDescriptor.GetProperties(type);

    Console.Write('{');
    Console.Write(type.Name);

    if (properties.Count != 0)
    {
        Console.Write(' ');

        for (int i = 0, n = properties.Count; i < n; i++)
        {
            if (i != 0)
                Console.Write("; ");

            var property = properties[i];

            Console.Write(property.Name);
            Console.Write(" = ");
            Console.Write(property.GetValue(o));
        }
    }

    Console.WriteLine('}');
}
私有静态无效转储(对象o)
{
如果(o==null)
{
控制台。写线(“”);
返回;
}
var type=o.GetType();
var properties=TypeDescriptor.GetProperties(类型);
Console.Write('{');
Console.Write(type.Name);
如果(properties.Count!=0)
{
控制台。写入(“”);
for(int i=0,n=properties.Count;i
如果要转储字段而不是属性,可以使用
type.GetFields()
并对上述代码进行必要的修改
FieldInfo
具有类似的
GetValue()
方法


请注意,这不会打印记录的“深度”表示。为此,您可以将其调整为递归解决方案。您可能还希望支持集合/数组、引用字符串和标识循环引用。

下面是一个使用反射的简短示例:

void Main()
{
    var myObj = new SomeClass();
    PrintProperties(myObj);

    myObj.test = "haha";
    PrintProperties(myObj);
}

private void PrintProperties(SomeClass myObj){
    foreach(var prop in myObj.GetType().GetProperties()){
     Console.WriteLine (prop.Name + ": " + prop.GetValue(myObj, null));
    }

    foreach(var field in myObj.GetType().GetFields()){
     Console.WriteLine (field.Name + ": " + field.GetValue(myObj));
    }
}

public class SomeClass {
 public string test {get; set; }
 public string test2 {get; set; }
 public int test3 {get;set;}
 public int test4;
}
输出:

{
  "id": "42",
  "name": "Bob",
  "reg": "Foo",
  "data1": null
}
test: 
test2: 
test3: 0
test4: 0

test: haha
test2: 
test3: 0
test4: 0

仔细阅读反思,这是最简单的工作工具。你可以用反思来实现这一点。如果你搜索,你可以找到例子。如果你还没有这个问题的答案,那么你不应该发布答案。当你对这个问题有了一个实际的答案后,就发布一个答案。发布非答案只是为了获得更早的时间戳是不合适的行为。这只适用于属性,OP有字段。一个简单的修复方法(你有一个很好的反射库),只是需要注意的是它们不是正确的字段声明,所以我认为他只是在用速记输入。但是是的,你是对的。