如何获取字符串数组值,即作为对象数组中的参数传递?

时间:2017-06-15 09:17:24

标签: c# arrays

我试图在对象数组中传递字符串数组和其他参数,另一方面我想检索这些参数值并显示它们,但我无法检索字符串数组值,而是显示类型字符串数组。

static void Main(string[] args)
{
    string[] test = {"t1","t2","t3"};

    TestArray(1,test,"Hello");
}

static void TestArray(params object[] array)
{  
    foreach(var value in array)
    {
        Console.WriteLine(value);                    
    }
    Console.ReadLine();
}

4 个答案:

答案 0 :(得分:4)

You're printing all values as string. Array.ToString() will return $elementType[], so System.String[] in your case.

You'll need to test if value is an IEnumerable, and if so, iterate over it and print its members' values, recursively.

static void TestArray(params object[] array)
{  
    PrintValue(value);       
}

public void PrintValue(object value)
{
    var enumerable = value as IEnumerable;
    if (enumerable != null)
    {
        foreach (var subvalue in enumerable)
        {
            PrintValue(subvalue);
        }
    }
    else
    {
        Console.WriteLine(value.ToString());
    }
}

Do note that this still can't print complex types, and in that case, will just output its type name again.

答案 1 :(得分:1)

Try this:

    static void Main(string[] args)
    {
        string[] test = { "t1", "t2", "t3" };

        TestArray(1, test, "Hello");
    }

    static void TestArray(params object[] array)
    {
        foreach (var value in array)
        {
            if (value is IEnumerable<object>)
                foreach (var element in value as IEnumerable<object>)
                    Console.WriteLine(element);
            else
                Console.WriteLine(value);
        }
        Console.ReadLine();
    }

答案 2 :(得分:0)

You have to check if the value is an array and loop over it:

public static void DisplayParams(params object[] parameters)
{
    foreach(var param in parameters)
    {
        var arr = param as string[];
        if( arr  != null)
        {
            foreach(var value in arr)
            {
                Console.WriteLine( value );
            }
        }
        else
        { 
            Console.WriteLine( param );
        }
    }
}

This part:

var arr = p as string[];
if( arr  != null)

will check if its an array and will loop over it when it is.

Of course when you insert other types this won't work, so some validation would be wise

答案 3 :(得分:0)

One option to consider:

    static void Main(string[] args)
    {
        string[] test = { "t1", "t2", "t3" };

        List<object> combined = new List<object> {1};
        combined.AddRange(test);
        combined.Add("Hello");

        TestArray(combined.ToArray());
    }

    static void TestArray(params object[] array)
    {
        foreach (var value in array)
        {
            Console.WriteLine(value);
        }
        Console.ReadLine();
    }

In short, you merge all the values into a single array before passing that array into the method.

If you went this route, consider removing the params keyword - so it makes it literally impossible for someone to call it the original way you had it. It forces them to think about how they want IEnumerables to be handled.