我有这个代码我正在尝试从C#转换为VB.net:
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
// Create a new, STA thread
object[] staOutputs = null;
object retval = null;
Thread thread = new Thread(
delegate(){
retval = _innerInvoker.Invoke(instance, inputs, out staOutputs);
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
outputs = staOutputs;
return retval;
}
整个代表的事情让我失望。任何人都可以帮我转换吗?或者是否有一篇文章解释了如何做到这一点?
答案 0 :(得分:8)
这是一个匿名函数,VB.Net不支持这样的匿名函数(假设.Net 2.0,因为在.Net 3.5中将编写为lambda表达式)。
你可以在VB.Net中做的最好的事情是在同一个类中将匿名函数(委托)作为一个单独的方法添加,并使用AddressOf运算符来引用这个新方法。
<强>更新强>
再次读取代码,翻译很复杂,因为您的匿名方法将被解释为闭包,这意味着C#编译器在将代码转换为IL之前对代码进行了复杂的转换;创建一个全新的类是捕获(关闭)方法中引用的局部变量:
Class InnerInvokerClosure
Public instance As Object
Public inputs() As Object
Public staOutputs() As Object
Public retValue As Object
Public _innerInvoker As SomeDelegateType
Sub New(ByRef instance as Object, ByRef inputs() as Object, ByRef staOutputs() As Object, ByRef retValue As Object, ByRef _innerInvoker As SomeDelegateType)
Me.instance = instance
Me.inputs = inputs
Me.staOoutputs = staOutputs
Me.retValue = retValue
Me._innerInvoker = _innerInvoker
End Sub
Public Function Invoke() As Object
retValue = _innerInvoker.Invoke(instance, inputs, staOutputs);
End Function
End Class
Public Function Invoke(ByVal instance As Object, ByVal inputs() as Object, ByRef outputs() As Object) As Object
Dim closure As New InnerInvokerClosure(instance, inputs, Nothing, Nothing, _innerInvoker)
Dim t As New Thread(AddressOf closure.Invoke)
t.SetApartmentState(ApartmentState.STA)
t.Start()
t.Join()
outputs = closure.staOutputs
return closure.retValue
End Function
请注意,此翻译未经测试且可能存在错误:确切的转换可能非常复杂。
答案 1 :(得分:6)
我最喜欢这样做的方法是在C#中编译,然后使用反射器来查看其他dotNET语言的外观。
Friend Class Program
' Methods '
Private Shared Function Foo(ByVal instance As Object, ByVal inputs As Object(), <Out> ByRef outputs As Object()) As Object
outputs = Nothing
Return Nothing
End Function
Public Function Invoke(ByVal instance As Object, ByVal inputs As Object(), <Out> ByRef outputs As Object()) As Object
Dim staOutputs As Object() = Nothing
Dim retval As Object = Nothing
Dim thread As New Thread(Function
retval = Me._innerInvoker.Invoke(instance, inputs, staOutputs)
End Function)
thread.SetApartmentState(ApartmentState.STA)
thread.Start
thread.Join
outputs = staOutputs
Return retval
End Function
' Fields '
Private _innerInvoker As MyInvoker = New MyInvoker(AddressOf Program.Foo)
End Class
<CompilerGenerated> _
Private NotInheritable Class <>c__DisplayClass1
' Methods '
Public Sub <Invoke>b__0()
Me.retval = Me.<>4__this._innerInvoker.Invoke(Me.instance, Me.inputs, Me.staOutputs)
End Sub
' Fields '
Public <>4__this As Program
Public inputs As Object()
Public instance As Object
Public retval As Object
Public staOutputs As Object()
End Class
答案 2 :(得分:1)
SOmethiong喜欢以下应该:(哪里_是私人领域(这不是生产,但希望完全给你这个想法)
Public Function Invoke(instance As Object, inputs As Object(), ByRef outputs As Object()) As Object
' Create a new, STA thread
Dim staOutputs As Object() = Nothing
Dim retval As Object = Nothing
Dim thread As New Thread(AddressOf(Do))
_instance = instance
_inputs = inputs
_staOutPuts = staOutputs
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
outputs = staOutputs
Return retval
End Function
private Function Do() as Object
Return _innerInvoker.Invoke(_instance, _inputs, _staOutputs)
end Function
答案 3 :(得分:1)
以下是我使用的最终代码:
Public Function Invoke(ByVal instance As Object, ByVal inputs As Object(), ByRef outputs As Object()) As Object Implements System.ServiceModel.Dispatcher.IOperationInvoker.Invoke
Dim staOutputs As Object() = Nothing
Dim retval As Object = Nothing
Dim thread As New Thread(AddressOf DoWork)
_instance = instance
_inputs = inputs
_staOutPuts = staOutputs
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
outputs = staOutputs
Return retval
End Function
Private Function DoWork() As Object
Return _innerInvoker.Invoke(_instance, _inputs, _staOutPuts)
End Function
@Rune FS - 如此接近,只需要一点帮助。谢谢!
答案 4 :(得分:0)
位于http://converter.telerik.com/
的在线C#到VB转换器(反之亦然)我将你的代码粘贴在那里....我不知道这是否是有效的VB或者它是否可行,但你可以试一试。
Public Function Invoke(instance As Object, inputs As Object(), ByRef outputs As Object()) As Object
' Create a new, STA thread
Dim staOutputs As Object() = Nothing
Dim retval As Object = Nothing
Dim thread As New Thread(Function() Do
retval = _innerInvoker.Invoke(instance, inputs, staOutputs)
End Function)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
outputs = staOutputs
Return retval
End Function