简体   繁体   English

如何创建一个接受任何对象并为所有属性返回“名称:值”对的方法?

[英]How to create a method that takes any object and returns a name:value pair for all properties?

For example I have a simple class like 例如我有一个简单的类

public class Person{ 
    public int Age {get;set;}
    public string Name {get;set;}
}

I need to make a method that takes any class and spits out values of properties set in the object as a string in format "Age:35;Name:John Doe;" 我需要制作一个方法,该方法可以接受任何类,并以字符串形式“ Age:35; Name:John Doe;”吐出对象中设置的属性值。

I am looking for a method signature on the lines of 我正在寻找一种方法签名

public string SpitNameValuePairs<T>() 

or something like it. 或类似的东西。 How can this be done efficiently if using reflection? 如果使用反射,如何有效地做到这一点?

Here is a quick implementation. 这是一个快速实施。

    public static string SplitNameValuePairs(object value)
    {
        string retVal = string.Empty;
        List<string> keyValuePairs = new List<string>();

        foreach (var propInfo in value.GetType().GetProperties())
        {
            keyValuePairs.Add(string.Format("{0}:{1};", propInfo.Name, propInfo.GetValue(value, null).ToString()));
        }

        retVal = string.Join("", keyValuePairs.ToArray());

        return retVal;
    }

Then called like this: 然后像这样调用:

        var person = new Person();
        person.Name = "Hello";
        person.Age = 10;
        Console.WriteLine(SplitNameValuePairs(person));

That have protection from crashes when property have an indexer defined and also it output only instance properties (no static). 当属性定义了索引器并且仅输出实例属性(非静态)时,可以防止崩溃。

    private static string SplitNameValuePairs<T>(T value)
    {
        StringBuilder sb = new StringBuilder();

        foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
        {
            if (property.GetIndexParameters().Length == 0)
                sb.AppendFormat("{0}:{1};", property.Name, property.GetValue(value, null));
        }
        return sb.ToString();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GetHashCode为不同的对象返回相同的值。 是否有通过特定属性标识对象的方法? - GetHashCode returns the same value for different objects. Is there any method to identify object by particular properties? 从名称/值对目录快速创建对象属性? - Create object attributes on the fly from a name/value pair catalogue? 将ref类型作为参数并对其进行操作的void方法与将ref返回到同一对象的方法之间有什么区别吗? - Is there any difference between a void method that takes a ref type and manipulates it, and a method that returns a ref to the same object 如何编写一个将枚举作为泛型并返回int值的方法 - How to write a method that takes an enum as a generic and returns the int value 如何打印所有嵌套对象属性名称 - How to print all nested object properties name 如何从JSON对象访问一个名称值对 - how to access one name value pair from a JSON object 我可以创建一个采用值类型或引用类型但始终返回可为空类型的泛型方法吗 - Can I create a generic method that takes a value type or a reference type but always returns a nullable type 如何创建方法而不返回任何值 - How to create method without returning any value A方法是应该从对象获取某些属性还是只取对象? - Should A method takes some properties from an object or just take the object? MongoDb C# 驱动程序投影返回名称和值对 - MongoDb C# driver projection returns name and value pair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM