在 vb.net 中使用 nservicebus

发布于 2024-11-16 23:55:43 字数 569 浏览 1 评论 0原文

我正在尝试将 nServiceBus PubSub .net4 示例转换为 vb,但我在某一时刻遇到了困难,我认为这是一个语言问题,但我想我应该询问专家。

有问题的代码来自发布者:

var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

当我尝试在 vb 中运行此代码时,

 Public Property Bus As IBus
 Dim eM As New EventMessage()
            eM = Bus.CreateInstance(Of IEvent)()

我收到一个对象引用未设置为对象实例的错误

接口上使用 new

,但随后我收到一条错误消息,指出我不能在 iBus 为any 的 关于如何解决这个问题的想法?

考虑到 c# 和 vb.net 之间的相似之处,我不敢相信这是不可能的?

任何想

法谢谢克里斯

欢迎

Im trying to convert the nServiceBus PubSub .net4 example into vb and I'm struggling at one point which I think is a language issue but I thought I would ask the experts.

The code in question is from the publisher:

var eventMessage = publishIEvent ? Bus.CreateInstance<IEvent>() : new EventMessage();

When I try and run this in vb with

 Public Property Bus As IBus
 Dim eM As New EventMessage()
            eM = Bus.CreateInstance(Of IEvent)()

I get a object refrence not set to an instance of the object error

But then I get an error saying I cant use new on an interface which iBus is

any ideas on how I get around this?

Given the similarities between c# and vb.net I cant believe this isnt possible?

Any ideas welcome

Thanks

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白馒头 2024-11-23 23:55:43

条件的两个部分没有相同的类型,但它们都可以分配给 IEvent (我相信),这是 C# 编译器将使 eventMessage 具有的类型。试试这个:(

Dim eM as IEvent
If publishIEvent Then
    eM = Bus.CreateInstance(Of IEvent)()
Else
    eM = New EventMessage()
End If

可能不完全正确的语法;我的 VB 越来越生锈了...)

(顺便说一句,我建议使用名称 eventMessage 而不是 eM .)

The two parts of the conditional do not have the same type, but they are both assignable to IEvent (I believe), which is the type the C# compiler will make eventMessage have. Try this:

Dim eM as IEvent
If publishIEvent Then
    eM = Bus.CreateInstance(Of IEvent)()
Else
    eM = New EventMessage()
End If

(probably not entirely correct syntax; my VB is getting rusty...)

(By the way, I'd suggest using the name eventMessage in stead of eM.)

你是我的挚爱i 2024-11-23 23:55:43

上面的 C# 代码是一个 if-then 结构。我面前没有代码,但该行本质上是以下内容的简写:

If (publishIEvent == true)
{
   var eventMessage = Bus.CreateInstance<IEvent>()
}
else
{
   var eventMessage = new EventMessage();
}

希望这有助于解决问题。

仅供参考,我意识到上面的代码在语法上不正确,只是试图说明 C# 语句的要点。

The C# code above is a if-then structure. I don't have the code in front of me, but the line is essentially shorthand for:

If (publishIEvent == true)
{
   var eventMessage = Bus.CreateInstance<IEvent>()
}
else
{
   var eventMessage = new EventMessage();
}

Hope this helps solve the issue.

FYI, I realize the code above is not syntactically correct, just trying to illustrate the point of the C# statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文