动态传递参数 [英] Pass parameter dynamically

查看:68
本文介绍了动态传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将列表传递给下面的函数。



  public   static 列表< List< T>> AllCombinationsOf< T>( params  List< T> [] sets)
{
// 这里的一些代码
}



现在我通过使用以下方法调用该函数:

  var  x = AllCombinationsOf(A,B,C); 





我不知道运行前需要传递的列表数量。它会动态变化。如何动态创建列表并传递此函数。



如果我想将参数传递给AllCombinationsOf为

 AllCombinationsOf(A,B,C ,d); 
AllCombinationsOf(A,B);
AllCombinationsOf(A,B,C,D,E);

意味着我必须做什么?



我必须动态传递参数(将要更改的参数)



我试图将列表添加到此

列表与LT;列表与LT;串GT;> lst =  new 列表< List<字符串>>(); 



但它不起作用

解决方案

您只需列出值:

 List< string> l1 =  new 列表< string> {  heelo 再见}; 
List< string> l2 = new 列表< string> { heelo 再见};
List< int> l3 = new List< int> { 1 2 3 ,< span class =code-digit> 4 };
var x = AllCombinationsOf(l1,l2);
...

public static 列表< List< T> ;> AllCombinationsOf< T>( params List< T> [] sets)
{
Console.WriteLine(sets.Length);
return sets.ToList();
}

显然,你不能将第三个列表添加到参数中,但如果只列出你的参数,它们将作为List-of-列表传递字符串


I have to pass the list to below function.

public static List<List<T>> AllCombinationsOf<T>(params List<T>[] sets)
{ 
      //some set of code here
}


Now I call the function by using:

var x = AllCombinationsOf(A,B,C);



I don't know the number of list that I need to pass before runtime. It will be change dynamically. How to I create the list dynamically and pass this function.

If I want to pass parameter to "AllCombinationsOf" as

AllCombinationsOf(A,B,C,D); 
AllCombinationsOf(A,B);
AllCombinationsOf(A,B,C,D,E);

means what I have to do?

I have to pass parameter as dynamically( parameter that will be change)

I tried to add list into this

List<List<string>> lst = new List<List<string>>();


but it not working

解决方案

You just list the values:

    List<string> l1 = new List<string> { "heelo", "goodbye" };
    List<string> l2 = new List<string> { "heelo", "goodbye" };
    List<int> l3 = new List<int> { 1, 2, 3, 4 };
    var x = AllCombinationsOf(l1, l2);
    ...

public static List<List<T>> AllCombinationsOf<T>(params List<T>[] sets)
    {
    Console.WriteLine(sets.Length);
    return sets.ToList();
    }

Obviously, you can't add the third list into the parameters, but if you just list your params, they will be passed as an array of List-of-strings


这篇关于动态传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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