简体   繁体   中英

implicit and explicit conversion

I start a thread like this:

ThreadPool.QueueUserWorkItem(new WaitCallback(SetupThread), (object)new object[] { IP, lc });

To comit the arguments I used the object. IP is easy to recover, but lc ist a list. How can I acess this argument?

    private void SetupThread(object obj)
    {
        object[] arg = obj as object[];
        String IPadress = Convert.ToString(arg[0]);
        List<String> listcom = arg[1];

        theNDDssh.RunListCom(IPadress,
                             listcom,
                                    sshqueue,
                                    error
                            );

The Compiler says that there is a explicit conversion. arg[1] can not be converted implicit.

List<string> listcom = (List<string>)arg[1];

Its a simple cast. You can do the same with the first argument instead of the Convert call.

Alternatively, you could use a Tuple<T,T> :

ThreadPool.QueueUserWorkItem(new WaitCallback(SetupThread), Tuple.Create(IP, lc));

Cast it back in the target method, and then just access each item inside it:

private void SetupThread(object obj)
{
    var args = (Tuple<string, List<string>>)obj;

    String IPadress = args.Item1;
    List<String> listcom = args.Item2;

    ...

您需要将其转换为

(List<String>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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