object to string array

可紊 提交于 2019-12-17 22:39:11

问题


I am trying to convert an object (is declared here as 'obj': object is array, primitive) to a string array.

object can be anything uint[], int16[], etc.

I have been trying to use

string[] str = Array.ConvertAll<object, string>((object[])obj, Convert.ToString);

The problem occurs when I try to cast the unknown type object into object[]. I have been getting casting error.

One attempt I made, which failed, was using

object[] arr = (object[])obj;

or

IEnumerable<object> list = obj as IEnumerable<object>
object[] arr = (object[])list;

I saw postings regarding value type and reference type issue on casting.

Would there be a simple code that can handle casting to object[] regardless of type of object, as long as it is an array ? I am trying to avoid manual handling of every possible type casting.

thanks in advance


回答1:


You can use the fact that every array implements IEnumerable:

string[] arr = ((IEnumerable)obj).Cast<object>()
                                 .Select(x => x.ToString())
                                 .ToArray();

This will box primitives appropriately, before converting them to strings.

The reason the cast fails is that although arrays of reference types are covariant, arrays of value types are not:

object[] x = new string[10]; // Fine
object[] y = new int[10]; // Fails

Casting to just IEnumerable will work though. Heck, you could cast to Array if you wanted.




回答2:


If it's always a collection of some type (array, list, etc ...) then try casting back to plain old System.Collections.IEnumerable and go from there

string[] str = ((System.Collections.IEnumerable)obj)
  .Cast<object>()
  .Select(x => x.ToString())
  .ToArray();

Here is a more thorough implementation that handles non-collections as well

static string[] ToStringArray(object arg) {
  var collection = arg as System.Collections.IEnumerable;
  if (collection != null) {
    return collection
      .Cast<object>()
      .Select(x => x.ToString())
      .ToArray();
  }

  if (arg == null) {
    return new string[] { };
  }

  return new string[] { arg.ToString() };
}



回答3:


my example:

   public class TestObject
    {
        public string Property1 { get; set; }

        public string Property2 { get; set; }

        public string Property3 { get; set; }
    }



    static void Main(string[] args)
    {
        List<TestObject> testObjectList = new List<TestObject>
        {
            new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" },
            new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" },
            new TestObject() { Property1 = "1", Property2 = "2", Property3 = "3" }
        };

        List<string[]> convertedTestObjectList = testObjectList.Select(x => new string[] { x.Property1, x.Property2, x.Property3 }).ToList();
    }


来源:https://stackoverflow.com/questions/10745542/object-to-string-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!