0

So I have the following line of code in c# to register AutoMapper with SimpleInjector:

container.Register(() => config.CreateMapper(container.GetInstance));

For context, config.CreateMapper() looks like this:

public IMapper CreateMapper(Func<Type, object> serviceCtor);

While container.GetInstance looks like this:

public object GetInstance(Type serviceType);

The first line of code I posted works in C# because config.CreateMapper takes in a function that takes a type of Type and returns a type of object. Exactly what container.GetInstance does.

When I try to do this same line in VB, as:

container.Register(Function() config.CreateMapper(container.GetInstance))

It gives an error Type arguments for 'Public Function Container.GetInstance(Of TService)()' cannot be inferred from the usage. Try specifying type arguments explicitly. It is giving this error because container.GetInstance has an overload that looks like this:

public TService GetInstance<TService>() where TService : class;

Why does the VB try to use the wrong overload of GetInstance and how can I force it to use the correct one?

1
  • () are optional, right now it's trying to call the method GetInstance without any parameters. Commented Jul 6, 2017 at 16:57

2 Answers 2

1

Use AddressOf for getting function reference.

container.Register(Function() config.CreateMapper(AddressOf container.GetInstance))

In C# function with parenthesis executes it, function without parenthesis will return reference to the function.

In VB.NET parenthesis in function are optional - function will be executed anyway. So for referencing a function you should use AddressOf operator.
AddressOf Operator

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Learn something new everyday.
0

Use the AddressOf keyword before the function parameter to define this as Parameter as function pointer.

AddressOf ist the same as @ keyword in C#. The keyword is implicit used in your c# sample.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.