简体   繁体   English

如何从一个方法返回多个变量?

[英]How can I return from a method more than one variable?

This method returns only the process filename: 此方法仅返回进程文件名:

public static string GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return proc.MainModule.FileName.ToString();
}

But I want to return also the process name: 但是我想要返回进程名称:

proc.ProcessName;

I believe you have four options (in preference order) 我相信你有四种选择(按优先顺序)

  • Return proc.MainModule directly and extract necessary information from caller. 直接返回proc.MainModule并从调用者中提取必要的信息。
public static ProcessModule GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return proc.MainModule;
}
  • Create a class containing both values and return that 创建一个包含两个值的类并返回该值
public class ProcessInformation
{
    public string FileName;
    public string ProcessName;
}

public static ProcessInformation GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    var pi = new ProcessInformation 
    {  
        proc.MainModule.FileName,
        proc.MainModule.ProcessName
    }
    return pi;
}
  • Return a tuple from method Tuple<string, string> 从方法Tuple<string, string>返回一个元Tuple<string, string>
public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return return Tuple.Create(proc.MainModule.FileName,proc.MainModule.ProcessName);
}
  • Create 2 out parameters on your method (I never seen two out parameters implemented and I discourage this since definitively smells, but it's an option C# provides) 在你的方法上创建2 out参数(我从未见过实现过两个参数,我不鼓励这个,因为它确实有气味,但它是C#提供的一个选项)
string GetProcessInfo(IntPtr hwnd, out fileName, out processName)

You can create and return an object describing your result: 您可以创建并返回描述结果的对象:

public class ProcessInfo
{
    public string ProcessName { get; set; }
    public string FileName { get; set; }
}

public ProcessInfo GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);

    return new ProcessInfo 
    {
        FileName = proc.MainModule.FileName.ToString(),
        ProcessName = proc.ProcessName
    }
 }

Or (I personally like this less), a Tuple<string, string> : 或者(我个人更喜欢这个),一个Tuple<string, string>

public Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);

    return Tuple.Create(proc.MainModule.FileName.ToString(),
                        proc.ProcessName);
}

Since both are string , how about return a Tuple<string, string> instead? 由于两者都是string ,如何返回Tuple<string, string>呢?

public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    Tuple<string, string> t = new Tuple<string, string>
    (
         proc.MainModule.FileName,
         proc.ProcessName
    );
    return t;
}

Last option would be using out-params: 最后一个选项是使用out-params:

public voidstring GetProcessInfo(IntPtr hwnd, out string fileName, out string processName{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    fileName = proc.MainModule.FileName.ToString();
    processName = proc.ProcessName;
}

You could change the return type to Process : 您可以将返回类型更改为Process

    public static Process GetProcessInfo(IntPtr hwnd)
    {
        uint pid = 0;
        GetWindowThreadProcessId(hwnd, out pid);
        return Process.GetProcessById((int)pid);
     }

and then get the data you need from the returned object: 然后从返回的对象中获取所需的数据:

var proc = GetProcessInfo(hwnd);
var processName = proc.ProcessName;
var moduleFileName = proc.MainModule.FileName.ToString();

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

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