.NET反射 - 将数组类型的属性值转换为逗号分隔的字符串

时间:2021-12-13 16:28:23

I am inspecting .NET objects using reflection to dump them to a log.

我正在使用反射检查.NET对象以将它们转储到日志中。

I've hit a snag - what if a property is an array type? I do get the PropertyInfo structure for that property, and I can see that the property is an array, and I can even get the value of that array property:

我遇到了障碍 - 如果属性是数组类型会怎么样?我得到该属性的PropertyInfo结构,我可以看到该属性是一个数组,我甚至可以得到该数组属性的值:

if(propertyInfo.PropertyType.IsArray)
{
    var value = propertyInfo.GetValue(myObject, null);
}

but now I'm stuck. When I look in the Visual Studio debugger, it shows as int[3] - so VS knows it's an array of three integers - but how can I convert this to a comma-separated string representing these three integers now?

但现在我被卡住了。当我查看Visual Studio调试器时,它显示为int [3] - 所以VS知道它是一个包含三个整数的数组 - 但是如何将它转换为一个逗号分隔的字符串,表示现在这三个整数?

I tried things like

我尝试过类似的东西

string.Join(", ", value);

and others - but I'm always struggling with the fact that value is "dynamic", e.g. it could be a int[] or a decimal[] or even something else - so I cannot statically type it.... and if I don't type it, then the string.Join() returns strange results (definitely not what I'm looking for...)

和其他人 - 但我总是在努力应对价值“动态”的事实,例如:它可能是一个int []或十进制[]甚至是其他东西 - 所以我不能静态输入它....如果我不输入它,那么string.Join()会返回奇怪的结果(绝对不是什么我在找...)

Is there any clever way to convert this "array of something" to a comma-separated string without a lot of if(...) and else { .... } clauses??

是否有任何聪明的方法将这个“数组的东西”转换为逗号分隔的字符串,而没有很多if(...)和其他{....}子句?

Somehow I'm in a brain-freeze here right now - any ideas to thaw this freeze up would be most welcome!

不知怎的,我现在正处于大脑冻结状态 - 任何解冻这种冻结的想法都会受到欢迎!

2 个解决方案

#1


6  

Well it seems like the easiest way around this problem would be to convert it to an IEnumerable<object>. Like this:

好吧,似乎解决这个问题的最简单方法是将其转换为IEnumerable 。喜欢这个:

if(propertyInfo.PropertyType.IsArray)
{
    var values = (IEnumerable)propertyInfo.GetValue(myObject, null);
    var stringValue = string.Join(", ", values.OfType<object>());
}

Although, because of array covariance in c#, if values is an array of a reference type, it should be castable to object[]. For this case, you could use this instead:

虽然,由于c#中的数组协方差,如果values是引用类型的数组,它应该可以转换为object []。对于这种情况,您可以使用此代替:

if(propertyInfo.PropertyType.IsArray)
{
    var values = (IEnumerable)propertyInfo.GetValue(myObject, null);
    var elementType = propertyInfo.PropertyType.GetElementType();
    if (elementType != null && !elementType.IsValueType)
    {
        var stringValue = string.Join(", ", (object[])values);
    }
    else
    {
        var stringValue = string.Join(", ", values.OfType<object>());
    }
}

#2


2  

This is the simplest solution that would work for all types of collections including arrays (IEnumerable is the least common base we can come up with):

这是最简单的解决方案,适用于所有类型的集合,包括数组(IEnumerable是我们可以提出的最不常见的基础):

string.Join(", ", ((IEnumerable)value).Cast<object>().Select(i => i.ToString()));

string.Join(“,”,((IEnumerable)value).Cast ()。选择(i => i.ToString()));

#1


6  

Well it seems like the easiest way around this problem would be to convert it to an IEnumerable<object>. Like this:

好吧,似乎解决这个问题的最简单方法是将其转换为IEnumerable 。喜欢这个:

if(propertyInfo.PropertyType.IsArray)
{
    var values = (IEnumerable)propertyInfo.GetValue(myObject, null);
    var stringValue = string.Join(", ", values.OfType<object>());
}

Although, because of array covariance in c#, if values is an array of a reference type, it should be castable to object[]. For this case, you could use this instead:

虽然,由于c#中的数组协方差,如果values是引用类型的数组,它应该可以转换为object []。对于这种情况,您可以使用此代替:

if(propertyInfo.PropertyType.IsArray)
{
    var values = (IEnumerable)propertyInfo.GetValue(myObject, null);
    var elementType = propertyInfo.PropertyType.GetElementType();
    if (elementType != null && !elementType.IsValueType)
    {
        var stringValue = string.Join(", ", (object[])values);
    }
    else
    {
        var stringValue = string.Join(", ", values.OfType<object>());
    }
}

#2


2  

This is the simplest solution that would work for all types of collections including arrays (IEnumerable is the least common base we can come up with):

这是最简单的解决方案,适用于所有类型的集合,包括数组(IEnumerable是我们可以提出的最不常见的基础):

string.Join(", ", ((IEnumerable)value).Cast<object>().Select(i => i.ToString()));

string.Join(“,”,((IEnumerable)value).Cast ()。选择(i => i.ToString()));