views:

95

answers:

3

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.

+1  A: 

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 ...

Blam
A: 

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.

Chris Tavares
A: 

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

shannon.stewart