gpt4 book ai didi

c# - 帮助将小型 C# WCF 应用程序转换为 Visual Basic(第 2 部分)

转载 作者:太空宇宙 更新时间:2023-11-03 11:43:46 26 4
gpt4 key购买 nike

我最近问了一个关于将我在互联网上找到的代码示例从 C# 翻译成 VB 的问题,我得到了自动代码翻译网站的链接 (http://converter.telerik.com/& http://www.developerfusion.com/tools/convert/csharp-to-vb/ ) .我是 VB 的新手,不幸的是,翻译人员返回的 VB 无法在 Visual Studio 2010 中编译。我希望有人可以帮助我修复 VB 代码的非工作部分。

这是我要转换成 VB 的 C# 代码:

namespace WCFCallbacks
{
using System;
using System.ServiceModel;

[ServiceContract(CallbackContract = typeof(IMessageCallback))]
public interface IMessage
{
[OperationContract]
void AddMessage(string message);

[OperationContract]
bool Subscribe();

[OperationContract]
bool Unsubscribe();
}

interface IMessageCallback
{
[OperationContract(IsOneWay = true)]
void OnMessageAdded(string message, DateTime timestamp);
}
}

namespace WCFCallbacks
{
using System;
using System.Collections.Generic;
using System.ServiceModel;

public class MessageService : IMessage
{
private static readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();

public void AddMessage(string message)
{

subscribers.ForEach(delegate(IMessageCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnMessageAdded(message, DateTime.Now);
}
else
{
subscribers.Remove(callback);
}
});
}

public bool Subscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
if (!subscribers.Contains(callback))
subscribers.Add(callback);
return true;
}
catch
{
return false;
}
}

public bool Unsubscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
if (!subscribers.Contains(callback))
subscribers.Remove(callback);
return true;
}
catch
{
return false;
}
}
}
}

我上面列出的两个转换器都返回相同的 VB 代码。不起作用的 VB 代码部分在 AddMessage 方法中。这是翻译器生成的 VB 代码:

Imports System.ServiceModel

Namespace WCFCallbacks

<ServiceContract(CallbackContract:=GetType(IMessageCallback))> _
Public Interface IMessage
<OperationContract()> _
Sub AddMessage(ByVal message As String)

<OperationContract()> _
Function Subscribe() As Boolean

<OperationContract()> _
Function Unsubscribe() As Boolean
End Interface

Interface IMessageCallback
<OperationContract(IsOneWay:=True)> _
Sub OnMessageAdded(ByVal message As String, ByVal timestamp As DateTime)
End Interface

End Namespace

Imports System
Imports System.Collections.Generic
Imports System.ServiceModel

Namespace WCFCallbacks

Public Class MessageService
Implements IMessage

Private Shared ReadOnly subscribers As New List(Of IMessageCallback)()

Public Sub AddMessage(ByVal message As String) Implements IMessage.AddMessage

subscribers.ForEach(Function(callback As IMessageCallback) Do
If DirectCast(callback, ICommunicationObject).State = CommunicationState.Opened Then
callback.OnMessageAdded(message, DateTime.Now)
Else
subscribers.Remove(callback)
End If
End Function)
End Sub

Public Function Subscribe() As Boolean Implements IMessage.Subscribe
Try
Dim callback As IMessageCallback = OperationContext.Current.GetCallbackChannel(Of IMessageCallback)()
If Not subscribers.Contains(callback) Then
subscribers.Add(callback)
End If
Return True
Catch
Return False
End Try
End Function

Public Function Unsubscribe() As Boolean Implements IMessage.Unsubscribe
Try
Dim callback As IMessageCallback = OperationContext.Current.GetCallbackChannel(Of IMessageCallback)()
If Not subscribers.Contains(callback) Then
subscribers.Remove(callback)
End If
Return True
Catch
Return False
End Try
End Function

End Class

End Namespace

如果有人可以帮助我修复 AddMessage 方法的 VB 代码,我将不胜感激。谢谢!

最佳答案

我不是 VB.NET 程序员,但我认为 C# -> VB.NET 解析器显然无法处理该委托(delegate)技巧。代码中发生的事情实际上是非常基本的,如果你用委托(delegate)去除了那层复杂性(只有当你完全理解它时才看起来很简单)。最后,C# 代码只是遍历订阅者列表并调用它们。使用委托(delegate)的原因可能是能够从与正在迭代的集合相同的集合中删除,而不必将它们保存在另一个构造中。

在这里,我只是将要删除的订阅者添加到另一个列表,然后遍历从原始列表(未测试)中删除的订阅者:

    Public Sub AddMessage(ByVal message As String) Implements IMessage.AddMessage
Dim msgCallback As IMessageCallback

Dim removedSubscribers As New List(Of IMessageCallback)()

For Each msgCallback In subscribers
If (CType(msgCallback, ICommunicationObject).State = CommunicationState.Opened) Then
msgCallback.OnMessageAdded(message, DateTime.Now)
Else
removedSubscribers.Add(msgCallback)
End If
Next

For Each msgCallback In removedSubscribers
subscribers.Remove(msgCallback)
Next
End Sub

关于c# - 帮助将小型 C# WCF 应用程序转换为 Visual Basic(第 2 部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3961831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com