从转换为void返回委托的匿名函数返回

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从转换为void返回委托的匿名函数返回相关的知识,希望对你有一定的参考价值。

我正在努力返回用户关注的当前Active Directory的确切路径然后我找到了一些代码,虽然它们似乎都没有正常工作并且有bug。但是这段代码似乎有用...无论如何我想返回上面提到的路径(在这段代码中称为currDirectory;当我在此代码中将void类型更改为字符串类型并使用return currDirectory时,我收到错误

从匿名函数返回转换为void返回委托不能返回值

任何人都可以更改此代码,以便它可以将currDirectory作为字符串返回吗?

class Class2
{
    public static void Main()
    {
        RefreshWindow();  
    }

    public static string RefreshWindow()
    {
        Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
        Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);

        object shellApplication = Activator.CreateInstance(shellApplicationType);
        object windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });

        Type windowsType = windows.GetType();
        object count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);

        Parallel.For(0, (int)count, i =>
        {
            object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
            Type itemType = item.GetType();
            string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);

            if (itemName == "Windows Explorer" || itemName == "File Explorer")
            {
                string currDirectory = HttpUtility.htmlEncode((string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null)).Replace("///", @"").Replace("/", @"").Replace("%20", " ").Replace(@"file:", "");

                Console.WriteLine(currDirectory);
                Console.Read();

                return currDirectory;
            }
        });
    }
}
答案

您的代码不会返回单个路径,它将返回所有打开的Windows资源管理器实例,因此如果您打开多个,将返回所有内容,无论如何看看下面的解决方案,它应该可以解决您的问题。

public static string[] RefreshWindow()
{

    Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
    Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);

    object shellApplication = Activator.CreateInstance(shellApplicationType);
    object windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });

    Type windowsType = windows.GetType();
    var count = (int)windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);

    string[] currentDirectories = new string[count];
    Parallel.For(0, count, i =>
    {
        object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
        Type itemType = item.GetType();
        string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
        if (itemName == "Windows Explorer" || itemName == "File Explorer")
        {
            string currDirectory = HttpUtility.HtmlEncode((string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null)).Replace("///", @"").Replace("/", @"").Replace("%20", " ").Replace(@"file:", "");

            currentDirectories[i] =  currDirectory;
        }

    });
    return currentDirectories;
}

结果应该是这样的:enter image description here

以上是关于从转换为void返回委托的匿名函数返回的主要内容,如果未能解决你的问题,请参考以下文章

C#控制台基础 返回类型为void的无参数委托与匿名方法

如何使用匿名方法返回值?

如何使用返回数据的回调函数设置匿名块

没有返回类型的函数委托

委托泛型委托多播委托匿名函数lamda表达式事件

C# 委托 线程