1

I have been implementing Dependency Injection into an existing Winforms project and it has been going well so far, however I want to generalise the calling of the Forms, specifically the varying quantity of constructor parameters.

My code is as follows:

Public Shared Function GetForm(formObject As BaseObject, _
    parameters As Dictionary(Of String, Object)) As Form

        Select Case formObject.GetType()
            Case GetType(Production.Task)
                Return SMKernel.Kernel.Get(Of Forms.Production.Domain.ManageTask) _
                    (New Parameters.ConstructorArgument() _
                    {New Parameters.ConstructorArgument("task", _
                        CType(formObject, Production.RequiredTask))})
        End Select

    End Function

This works fine, the interface(s) are injected correctly, the constructor parameter "task" is populated and the Form works as expected.

As you can see I have a Dictionary that can contain several parameters which I need to add to the ConstructorArgument part of the Get method. Looking at the IntelliSense, I should be able to pass in an array of ConstructorArgument objects, however no matter what I have tried, it doesn't seem to work for one reason or another.

How do you accomplish this in Ninject if it is at all possible. If this way isn't possible, how can you pass multiple parameters into a Form's constructor via Ninject?

4

1 回答 1

2

使用已经提供的 ResolutionExtensions

    public static T Get<T>(this IResolutionRoot root, params IParameter[] parameters)

然后将它与 LINQ 结合起来

Kernel.Get(parameters.Select(kvp => new ConstructorArgument(kvp.Key, kvp.Value)).ToArray())

提供更多细节,我们可能会为您提供更优雅的方法。

于 2013-01-06T19:04:54.983 回答