#c# #vb.net
#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. \bacserveruserssatbrdocumentsvisual studio 2010ProjectsEncomendasEncomendasForm1.vb 60 21 Encomendas
В чем проблема?
Спасибо,
Комментарии:
1. В c # вы преобразуетесь в (строку). Чтобы сделать эквивалент в VB.Net , используйте CType(объект, строка)
Ответ №1:
Измените инициализацию «path» на:
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’ передает функцию без фактического вызова функции).