简体   繁体   English

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

[英]How to get string array values, i.e. passed as a parameter in an object array?

I am trying to pass string array and other parameters in an object array,and on other side I want to retrieve this parameter values and display them, but I am not able to retrieve the string array values,rather it displays the type of the string array. 我试图在对象数组中传递字符串数组和其他参数,另一方面,我想检索此参数值并显示它们,但是我无法检索字符串数组值,而是显示字符串的类型数组。

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();
}

You're printing all values as string. 您正在将所有值打印为字符串。 Array.ToString() will return $elementType[] , so System.String[] in your case. Array.ToString()将返回$elementType[] ,因此在您的情况下为System.String[]

You'll need to test if value is an IEnumerable , and if so, iterate over it and print its members' values, recursively. 您将需要测试value是否是IEnumerable ,如果是,则对其进行迭代并递归打印其成员的值。

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. 请注意,这仍然无法打印复杂类型,在这种情况下,将仅再次输出其类型名称。

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();
    }

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 当然,当您插入其他类型时,这将不起作用,因此进行一些验证是明智的

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. 如果您走了这条路线,请考虑删除params关键字-这样一来,实际上就不可能有人用您原来的方式来称呼它。 It forces them to think about how they want IEnumerables to be handled. 它迫使他们考虑如何处理IEnumerables。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取字符串名称来解析对象(即从它的意义上来说) - How to get a string name to resolve to an object (i.e. in the sense of what it is) 什么时候应该使用数组列表(即列表) <string []> =新清单 <string []> ())在C#中? - When should I use List of Array (i.e. List<string []> = new List<string []>()) in C#? 通过在 C# 中注入 object 值(即使用反射)来解析字符串公式 - Parsing a string formula by injecting object values (i.e. using reflection) in C# 从浮点数组中获取字节而无需分配(即通过强制转换) - Get bytes from float array without allocations (i.e. via cast) 使用.Net如何使用Sort方法反向排序数组,即Z到A? - Using .Net how do I use the Sort method to sort an Array in reverse i.e. Z to A? 如何将整数数组存储到C#中的字典中? 即是 <Integer, int[]> 可能? - How or is it possible to store an array of integers into a dictionary in C#? i.e. is <Integer, int[]> possible? 如何获取作为 func 传递的匿名方法的参数值? - How can i get the parameter values of an anonymous method passed as a func? 如何在C#中将字符串数组值转换为对象 - How do I convert string array values to object in c# 如何在C#中通过String调用构造函数(即通过Reflection)? - How to invoke Constructors by String in C# (i.e. by Reflection)? 如何获取这些xml值的字符串数组? - How do I get a string array of these xml values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM