Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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#_.net_Reflection_Properties_Typedescriptor - Fatal编程技术网

C# 反射-输出任何对象的最佳方式?

C# 反射-输出任何对象的最佳方式?,c#,.net,reflection,properties,typedescriptor,C#,.net,Reflection,Properties,Typedescriptor,可能重复: 如果我想要一个方法,它接受一个随机对象并输出(或以其他方式检索)每个包含的属性,那么哪条路最优雅、最健壮 这个问题源于我先前的问题和提出替代方法的评论 我以前的做法是,使用TypeDescriptor和PropertyDescriptor类: public static void extract(object obj) { List<string> properties = new List<string>(); foreach (Prop

可能重复:

如果我想要一个方法,它接受一个随机对象并输出(或以其他方式检索)每个包含的属性,那么哪条路最优雅、最健壮

这个问题源于我先前的问题和提出替代方法的评论

  • 我以前的做法是,使用
    TypeDescriptor
    PropertyDescriptor
    类:

    public static void extract(object obj)
    {
        List<string> properties = new List<string>();
        foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(obj))
        {
            string name = descriptor.Name;
            object value = descriptor.GetValue(obj);
            properties.Add(name + " = " value);
        }
        if (properties.Count == 0)
            output(obj.ToString());
        else
            output(obj, string.Concat(properties));
    }
    
    publicstaticvoid提取(objectobj)
    {
    列表属性=新列表();
    foreach(TypeDescriptor.GetProperties(obj)中的PropertyDescriptor描述符)
    {
    字符串名称=描述符。名称;
    对象值=描述符.GetValue(obj);
    添加(名称+“=”值);
    }
    如果(properties.Count==0)
    输出(obj.ToString());
    其他的
    输出(obj,string.Concat(属性));
    }
    
  • 建议的备选方案使用Type.GetProperties():

    publicstaticvoid提取(objectobj)
    {
    列表属性=新列表();
    foreach(obj.GetType().GetProperties()中的PropertyInfo属性)
    {
    字符串名称=property.name;
    object value=property.GetValue(obj,null);
    添加(名称+“=”值);
    }
    如果(properties.Count==0)
    输出(obj.ToString());
    其他的
    输出(obj,string.Concat(属性));
    }
    
到目前为止,我还没有研究过反射,也不知道这两者有什么不同。两者之间有什么好处吗? 还有其他(更好的)方法吗?

公共静态类ObjectExtensions
public static class ObjectExtensions
{
    public static string Extract<T>(this T theObject)
    {
        return string.Join(
            ",",
            new List<string>(
                from prop in theObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                where prop.CanRead
                select string.Format("{0} = {1}",
                prop.Name,
                prop.GetValue(theObject, null))).ToArray());
    }
}
{ 公共静态字符串提取(此T对象) { 返回字符串( ",", 新名单( 从Object.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)中的prop 在哪里可以阅读 选择string.Format(“{0}={1}”, 道具名称, GetValue(theObject,null)).ToArray(); } }
public static class ObjectExtensions
{
    public static string Extract<T>(this T theObject)
    {
        return string.Join(
            ",",
            new List<string>(
                from prop in theObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
                where prop.CanRead
                select string.Format("{0} = {1}",
                prop.Name,
                prop.GetValue(theObject, null))).ToArray());
    }
}