从C#转换为VB时函数出错

时间:2019-03-11 14:25:53

标签: c# vb.net

通过在线转换器将C#中的功能转换为VB。基本上,此功能使您可以打开服务器上的文件夹。

C#中的原始代码:

 var doc = XDocument.Load("cl.xml");


            var folderToFind = TextBox_ov.Text;

            var paths = doc.Descendants("dir")
                .Where(dir => string.Equals(folderToFind, (string)dir.Attribute("name"), StringComparison.OrdinalIgnoreCase))
                .Select(dir => dir.AncestorsAndSelf().Select(el => (string)el.Attribute("name")).Reverse().Aggregate(string.Empty, Path.Combine))
            ;

            foreach (string path in paths)
            {
                Process.Start(new ProcessStartInfo
                {
                    FileName = path,
                    UseShellExecute = true,
                    Verb = "open"
                });
            }

代码已转换为VB:

Private Sub btn_open_Click(sender As System.Object, e As System.EventArgs) Handles btn_open.Click

    Dim doc = XDocument.Load("cl.xml")
    Dim folderToFind = "ov" + TextBox_ov.Text
    Dim paths = doc.Descendants("dir").Where(Function(dir) String.Equals(folderToFind, CStr(dir.Attribute("name")), StringComparison.OrdinalIgnoreCase)).[Select](Function(dir) dir.AncestorsAndSelf().[Select](Function(el) CStr(el.Attribute("name"))).Reverse().Aggregate(String.Empty, Path.Combine))

    For Each path As String In paths
        Process.Start(New ProcessStartInfo With {
            .FileName = path,
            .UseShellExecute = True,
            .Verb = "open"
        })
    Next
End Sub

我遇到此错误:

Error   1   Overload resolution failed because no accessible 'Select' can be called with these arguments:
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Xml.Linq.XElement, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Nested function does not have a signature that is compatible with delegate 'System.Func(Of System.Xml.Linq.XElement, Integer, TResult)'.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Xml.Linq.XElement, Integer, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Xml.Linq.XElement, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) in extension method 'Public Function Aggregate(Of TAccumulate)(seed As TAccumulate, func As System.Func(Of TAccumulate, String, TAccumulate)) As TAccumulate' defined in 'System.Linq.Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Extension method 'Public Function Select(Of TResult)(selector As System.Func(Of System.Xml.Linq.XElement, TResult)) As System.Collections.Generic.IEnumerable(Of TResult)' defined in 'System.Linq.Enumerable': Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. \\bacserver\users\satbr\documents\visual studio 2010\Projects\Encomendas\Encomendas\Form1.vb    60  21  Encomendas

出什么问题了?

谢谢

1 个答案:

答案 0 :(得分:0)

将“路径”的初始化更改为:

Dim paths = doc.Descendants("dir").Where(Function(dir) String.Equals(folderToFind, CStr(dir.Attribute("name")), StringComparison.OrdinalIgnoreCase)).Select(Function(dir) dir.AncestorsAndSelf().Select(Function(el) CStr(el.Attribute("name"))).Reverse().Aggregate(String.Empty, AddressOf Path.Combine))

据我所知,您所缺少的只是“ AddressOf”。 (具有上述更改的代码将为我编译)。

(使用“ AddressOf”传递函数而不实际调用函数)。