Unity 应用程序块 - 将示例从 C# 转换为 VB

发布于 2024-09-24 04:48:12 字数 1531 浏览 3 评论 0原文

我尝试将 WPF 入门工具包从 C# 转换为 VB.net,而且我做得非常好,除了一个领域...使用 Unity 应用程序块的依赖注入。

我有以下 C# 代码块:

            Type viewModelType = viewModelAssembly.GetType(action.ViewModelTypeName);

            var notificationPolicy = unity.AddNewExtension<Interception>()
                .RegisterType(typeof(BaseViewModel), viewModelType, action.Name)
                .Configure<Interception>()
                .SetDefaultInterceptorFor(viewModelType, new VirtualMethodInterceptor())
                .AddPolicy("NotificationPolicy");

            notificationPolicy.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set));
            notificationPolicy.AddCallHandler<NotifyPropertyChangedCallHandler>();

我自动转换为 vb.net:vb.net

Dim viewModelType As Type = viewModelAssembly.[GetType](action.ViewModelTypeName)

Dim notificationPolicy = unity.AddNewExtension(Of Interception()).RegisterType(GetType(BaseViewModel), viewModelType, action.Name).Configure(Of Interception)().SetDefaultInterceptorFor(viewModelType, New VirtualMethodInterceptor()).AddPolicy("NotificationPolicy")

notificationPolicy.AddMatchingRule(New PropertyMatchingRule("*", PropertyMatchingOption.[Set]))
notificationPolicy.AddCallHandler(Of NotifyPropertyChangedCallHandler)()

代码生成错误“Latebound 重载解析无法应用于“RegisterType”,因为访问实例是接口类型”,并且我没有知道我该如何解决这个问题。我对 Unity 的东西完全陌生,除了 MS 提供的片段之外,我找不到 vb 示例。任何帮助将不胜感激。

谢谢大家,

Ryan

编辑:根据 Blam,我添加了额外的括号,但我仍然遇到相同的错误。

I'm attempted to convert the WPF Starter Kit from C# to VB.net and I'm doing really well, except for one area... Dependency Injection using the Unity Application Block.

I have the following C# code block:

            Type viewModelType = viewModelAssembly.GetType(action.ViewModelTypeName);

            var notificationPolicy = unity.AddNewExtension<Interception>()
                .RegisterType(typeof(BaseViewModel), viewModelType, action.Name)
                .Configure<Interception>()
                .SetDefaultInterceptorFor(viewModelType, new VirtualMethodInterceptor())
                .AddPolicy("NotificationPolicy");

            notificationPolicy.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set));
            notificationPolicy.AddCallHandler<NotifyPropertyChangedCallHandler>();

That I auto-convert to vb.net:

Dim viewModelType As Type = viewModelAssembly.[GetType](action.ViewModelTypeName)

Dim notificationPolicy = unity.AddNewExtension(Of Interception()).RegisterType(GetType(BaseViewModel), viewModelType, action.Name).Configure(Of Interception)().SetDefaultInterceptorFor(viewModelType, New VirtualMethodInterceptor()).AddPolicy("NotificationPolicy")

notificationPolicy.AddMatchingRule(New PropertyMatchingRule("*", PropertyMatchingOption.[Set]))
notificationPolicy.AddCallHandler(Of NotifyPropertyChangedCallHandler)()

The vb.net code generates the error "Latebound overload resolution cannot be applied to 'RegisterType' because the accessing instance is an interface type" and I have no idea how I can fix this. I'm totally new to this Unity stuff, and I'm unable to find vb examples - aside from the fragments MS offers. Any help would be greatly appreciated.

Thanks all,

Ryan

EDIT: Per Blam, I added the extra bracket, but I still get the same error.

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

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

发布评论

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

评论(3

绿萝 2024-10-01 04:48:12

我不知道缺少括号时如何编译:

 Dim notificationPolicy = unity.AddNewExtension(Of Interception()) _
.RegisterType(GetType(BaseViewModel), viewModelType, action.Name) _
.Configure(Of Interception)() _
.SetDefaultInterceptorFor(viewModelType, New VirtualMethodInterceptor()) _
.AddPolicy("NotificationPolicy")

括号在这里:

AddNewExtension(Of Interception()).注册...

I don't see how that compiles when it's missing a bracket:

 Dim notificationPolicy = unity.AddNewExtension(Of Interception()) _
.RegisterType(GetType(BaseViewModel), viewModelType, action.Name) _
.Configure(Of Interception)() _
.SetDefaultInterceptorFor(viewModelType, New VirtualMethodInterceptor()) _
.AddPolicy("NotificationPolicy")

Bracket is here:

AddNewExtension(Of Interception()).Register ...

辞慾 2024-10-01 04:48:12

你的VB代码看起来没问题。这里可能有两个不同的问题。首先,你使用的是Unity 2.0吗?您的 .vb 文件顶部是否有“Imports Microsoft.Practices.Unity”? RegisterType 上的大多数重载都定义为接口上的扩展方法,如果没有此 Imports 语句,编译器将看不到它们。

第二个问题可能与此行有关:

Dim notificationPolicy = ...

请注意,此处没有类型,因此 VB 必须猜测。默认情况下,我认为它正在猜测对象,然后回到后期绑定的东西。

您在这里有几个选择。

首先,将“Option Infer On”添加到 .vb 文件的顶部。这将打开类型推断。

如果这不起作用,请更改代码以声明类型。在本例中,它是:

Dim notificationPolicy as PolicyDefinition = ...

或者最后,您可以简单地将最后两行代码链接在一起并忘记该变量。改为这样做:

unity.AddNewExtension(Of Interception()) _
    .RegisterType(GetType(BaseViewModel), viewMOdelType, action.Name) _
    .Configure(Of Interception)() _
    .SetDefaultInterceptorFor(viewModelType, new VirtualMethodInterceptor()) _
    .AddPolicy("NotificationPolicy") _
        .AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.[Set])) _
        .AddCallHandler(Of NotifyPropertyChangedCallHandler)()

希望这会有所帮助。

Your VB code looks ok. There could be two different issues here. First, are you using Unity 2.0? Does your .vb file have an "Imports Microsoft.Practices.Unity" at the top of it? Most of the overloads on RegisterType are defined as extension methods on the interface, without this Imports statement the compiler won't see them.

The second issue might be with this line:

Dim notificationPolicy = ...

Notice there's no type here, so VB has to guess. By default, I think it's guessing Object, and falling back to late bound stuff.

You have a couple options here.

First, add "Option Infer On" to the top of your .vb file. That'll turn on type inference.

If that doesn't work, change the code to declare the type. In this case, it'd be:

Dim notificationPolicy as PolicyDefinition = ...

Or finally, you could simply chain the last two lines of code together and forget about the variable. Do this instead:

unity.AddNewExtension(Of Interception()) _
    .RegisterType(GetType(BaseViewModel), viewMOdelType, action.Name) _
    .Configure(Of Interception)() _
    .SetDefaultInterceptorFor(viewModelType, new VirtualMethodInterceptor()) _
    .AddPolicy("NotificationPolicy") _
        .AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.[Set])) _
        .AddCallHandler(Of NotifyPropertyChangedCallHandler)()

Hope this helps.

小糖芽 2024-10-01 04:48:12

我使用 Red-Gate Reflector 来查看代码在不同语言中的外观。尽管有时它不会产生最好看的代码。

I use Red-Gate Reflector to see how code looks in different languages. Although sometimes it doesn't produce the best looking code.

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