简体   繁体   English

在 Function 中有一个 List 的动态参数

[英]Have a dynamic parameter for a List in Function

I have a SOAP APi which needs an XML string created to send as the request.我有一个 SOAP APi 需要创建一个 XML 字符串作为请求发送。 There are a number of different XML strings required.需要许多不同的 XML 字符串。

eg.例如。 the Customer and Item strings are quite different and there is no WDSL ability on this old software. Customer 和 Item 字符串完全不同,并且这个旧软件没有 WDSL 功能。

I have a function to check which API enpoint is being called, then get the action, uRL and envelope (XML) from predefined functions.我有一个 function 来检查正在调用哪个 API 点,然后从预定义函数中获取操作、uRL 和信封 (XML)。

private static (string URL, string action, string envelope) getTaskDetails(string task, 
List<Object> envelopeList)
        {
            string _action = "";
            string _URL = "";
            string _envelope = "";


            switch (task)
            {
                case "getCustomerDetails":
                    _action = minfosDetails.minfosCustomerAction;
                    _URL = minfosDetails.minfosCustomerURL;
                    _envelope = getCustomerDetails(envelopeList);
                    break;

                default:
                    
                    break;

            }

            return (_URL, _action, _envelope);
        }

The aobve is the (not working) code. aobve 是(不工作的)代码。

What I want is the List (which I know is wrong) to accept any LIST thrown at it.我想要的是 List(我知道这是错误的)接受抛给它的任何 LIST。

eg.例如。 I can send the customerList to it, or the ItemList or the staffList etc. Then when i run the switch, it will take the List and throw it into the subfunction to get the envolope created.我可以将 customerList、ItemList 或 staffList 等发送给它。然后当我运行开关时,它将获取 List 并将其放入子函数以创建 envolope。

EDIT: Question answered, my working code below: Using编辑:问题已回答,我的工作代码如下:使用

getTaskDetails<T>(string task, List<T> envelopeList)

To call the next function I used:拨打下一个 function 我用过:

_envelope = getCustomerDetails(envelopeList.Cast<ACustomer>());

You can try using generics so the method is going to change like this您可以尝试使用 generics,因此该方法将像这样改变

private static (string URL, string action, string envelope) getTaskDetails<T>(List<T> envelopeList)

then you can check for what T Type is being passed instead of using your parameter string task那么您可以检查传递的是什么 T 类型,而不是使用参数string task

It is not possible to simply cast List<object> to List<ACustomer> .不可能简单地将List<object>转换为List<ACustomer> You can create a new list instead.您可以改为创建一个新列表。

_envelope = getCustomerDetails(envelopeList.Cast<ACustomer>().ToList());

OR, you could change the signature of getCustomerDetails to accept IEnumerable instead of List :或者,您可以更改getCustomerDetails的签名以接受IEnumerable而不是List

public static string getCustomerDetails(IEnumerable<ACustomer> customerList)

And then the .ToList() part is not needed, and no new list is created:然后.ToList()部分,也不会创建新列表:

_envelope = getCustomerDetails(envelopeList.Cast<ACustomer>());

To use Cast extension method you have to be using System.Linq;要使用Cast扩展方法,您必须using System.Linq;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM