Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.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# 如何在C中获取对象属性名称和值的列表?_C#_String_Linq - Fatal编程技术网

C# 如何在C中获取对象属性名称和值的列表?

C# 如何在C中获取对象属性名称和值的列表?,c#,string,linq,C#,String,Linq,假设我有一个简单的类 public class Person { string firstName; string lastName; int age; .... additional properties } 然后我有一些代码,上面写着 person = new Person("bob", "Smith", 27); 林克有办法吗?我可以得到一个返回的字符串 姓鲍勃/n姓史密斯/n 27岁是的,试试这个: void Main() { var pers

假设我有一个简单的类

public class Person {
    string firstName;
    string lastName;
    int age;
    .... additional properties
}
然后我有一些代码,上面写着

person = new Person("bob", "Smith", 27);
林克有办法吗?我可以得到一个返回的字符串

姓鲍勃/n姓史密斯/n 27岁是的,试试这个:

void Main()
{
    var person = new Person("bob", "Smith", 27);

    var result = String.Join(Environment.NewLine, typeof(Person).GetFields().Select(p => $"{p.Name} {p.GetValue(person)}"));

    Console.WriteLine(result);
}

public class Person
{
    public Person(string firstName, string lastName, int age)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    public string firstName;
    public string lastName;
    public int age;
}
这就产生了:

firstName bob lastName Smith age 27
如果您只是想手工保存ToString的编码,那么您当然可以将这两种方法结合使用。

每个类都有一个ToString的默认实现。您可以用自己喜欢的任何方法覆盖此默认实现。对于您的情况,您可以尝试以下方法:

公共阶层人士 { public Personstring lastName、string lastName、int age { this.FirstName=FirstName; this.LastName=LastName; 这个。年龄=年龄; } 公共字符串名{get;set;} 公共字符串LastName{get;set;} 公共整数{get;set;} 公共重写字符串ToString=>$FirstName{FirstName}{Environment.NewLine}LastName{LastName}{Environment.NewLine}Age{Age}; } 然后在实例上调用ToString:

人=新人,莫拉莱斯,18岁; Console.WriteLineperson.ToString; 这将打印出:

名字迈尔斯 姓莫拉莱斯
18岁

纯LINQ用于序列。输入是IEnumerable/IQueryable序列,输出是Select、GroupBy、Join等中间结果的另一个序列,或LINQ语句ToList、TODIGINCIARY、Max、AGGRATE、Count的最终结果

但是,您可以用一种类似于LINQ的方式编写函数:您以一种格式编写它们,就像您的扩展类Person使用了一种新方法一样。看

将person转换为文本表示形式:

static string ToText(this Person person)
{
    return $"FirstName {person.firstName}/nLastName {person.lastName}/nAge {person.Age}";
}
用法:

Person person = ...
string txt = person.ToText();
IEnumerable<Person> persons = ...
var result = persons.ToText();
static 
如果要将其用于人员序列:

static IEnumerable<Person> ToText(this IEnumerable<Person> persons)
{
    return persons.Select(person => person.ToText();
}
用法:

Person person = ...
string txt = person.ToText();
IEnumerable<Person> persons = ...
var result = persons.ToText();
static 

最好的方法是使用namefperson.firstname,如果他没有任何其他字段,则可以使用,但如前所述,他有其他字段是的,他提到了属性,但从上下文来看,我认为他指的是字段。我会说覆盖ToString会更漂亮。它的输出更加灵活,没有那么复杂,并且完全适合该方法的用途。@Bleep Bloop-我怀疑OP没有Person类的控制权,但是我在回答中添加了一个ToString选项。