对象到数组转换或将对象转换为数组 [英] object to array casting or convert object to array

查看:99
本文介绍了对象到数组转换或将对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对象是函数中的参数。



i希望将其转换为字符串数组



怎么做

i have object as parameter in function.

i want to convert that to array of string

how to do that

推荐答案

下面介绍一个例子



One example is described on following below

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //object method casted to string array object
            string[] StringArray = Cast(GetStringArray(), new { MyList = new string[] { } }).MyList;
            foreach (string item in StringArray)
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
        static object GetStringArray()
        {
            //Creating a Annonymous array string type here which is returning a object type
            return new { MyList = new string[] { "Code", "Project", "Question", "Answer" } };
        }
        //Cast object to a specific return type.
        static T Cast<T>(object obj, T Type)
        {
            return (T)obj;
        }

    }

}


这篇关于对象到数组转换或将对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆