简体   繁体   English

带有项目的列表返回空

[英]List with items returns empty

I have created a simple List function but if I Loop through the List it's empty. 我已经创建了一个简单的List函数,但是如果我循环遍历List它就是空的。 It should not be! 不应该!

// List function 
    public class process_hook
    {
        public static List<String> pro_hook = new List<String>
                                              (new String[] { list_all_pocesses() });
        protected static string list_all_pocesses()
        {
            StringBuilder _list = new StringBuilder();
            foreach (Process i in Process.GetProcesses("."))
            {
                try
                {
                    foreach (ProcessModule pm in i.Modules)
                    {
                        pro_hook.Add(pm.FileName.ToString());
                    }
                }
                catch { }
            }
            return _list.ToString();
        }
    }


        // call 
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (String _list in process_hook.pro_hook)
            {
                Console.WriteLine(_list);
            }
        }

Well this is a problem to start with: 那么这是一个问题:

catch { }

If anything goes wrong, you'll just silently abort. 如果出现任何问题,你只会默默地中止。

Maybe that's what's happening? 也许这就是发生了什么? (EDIT: It is. See later.) (编辑:是的。见稍后。)

The next problem is that your "list" will only ever contain a single string... is that really what you intended? 接下来的问题是你的“列表”只会包含一个字符串...这真的是你想要的吗? I doubt that the list you're seeing is actually empty - but it will contain a single empty string. 我怀疑你看到的列表实际上是空的 - 但它将包含一个空字符串。

(As a side note, I would strongly suggest that you start following .NET naming conventions and avoid global variables like this.) (作为旁注,我强烈建议您开始遵循.NET命名约定并避免像这样的全局变量。)

EDIT: Aargh - I've just realized what you've done. 编辑:Aargh - 我刚刚意识到你做了什么。 You're probably actually getting a NullReferenceException in list_all_pocesses , which you've caught and ignored. 你可能实际上在list_all_pocesses得到了一个NullReferenceException ,你已经捕获并忽略了它。

Your call to pro_hook.Add is made before you've assigned a value to pro_hook . 在为pro_hook.Add指定值之前,您调用了pro_hook Basically you've got a variable initializer which uses a method which in turn uses the variable. 基本上你有一个变量初始化器,它使用一个方法反过来使用变量。 Don't do that. 不要那样做。 If you step through your code in the debugger, you may get more of an idea of what's going on, but basically you've created a big ball of spaghetti for yourself. 如果你在调试器中单步调试代码,你可能会更多地了解正在发生的事情,但基本上你已经为自己创建了一个大意大利面。

Why doesn't list_all_pocesses just return a List<string> ? 为什么list_all_pocesses只返回List<string> Why are you using a StringBuilder at all? 你为什么要使用StringBuilder

Well... you're returning an empty string builder. 嗯......你正在返回一个空的字符串生成器。 That's your problem. 那是你的问题。 Your code is doing what you're telling it to do. 您的代码正在执行您要告诉它的操作。 :) :)

 return _list.ToString();
public class process_hook
{
    public static List<string> pro_hook = list_all_pocesses();
    protected static List<string> list_all_pocesses()
    {
        List<string> list = new List<string>();

        foreach (Process i in Process.GetProcesses("."))
        {
            foreach (ProcessModule pm in i.Modules)
            {
                list.Add(pm.FileName.ToString());
            }
        }
        return list;
    }
}

_list.ToString() is not going to return any meaningful value. _list.ToString()不会返回任何有意义的值。 Try something like this instead: 尝试这样的事情:

public static List<string> pro_hook = list_all_processes();

protected static List<string> list_all_processes()
{
    var list = new List<string>();

    foreach (Process i in Process.GetProcesses(".")) {
        try {
            foreach (ProcessModule pm in i.Modules) {
                list.Add(pm.FileName);
            }
        } catch { }
    }

    return list;
}

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

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