Mediator
Mediator

Reputation: 15378

How use IoC for empty constructor

I use 3-party library and I want to invoke custom constructor extend of empty constructor.

I use structuremap.

Is it possible ?

Source code from 3-party library:

  public static T InstantiateType<T>(Type type)
            {
                if (type == null)
                {
                    throw new ArgumentNullException("type", "Cannot instantiate null");
                }
                ConstructorInfo ci = type.GetConstructor(Type.EmptyTypes);
                if (ci == null)
                {
                    throw new ArgumentException("Cannot instantiate type which has no empty constructor", type.Name);
                }
                return (T) ci.Invoke(new object[0]);
            }

I tried

 x.For<IJobFactory>().Use<StructureMapJobFactory>();
                x.ForConcreteType<StructureMapJobFactory>().Configure.SelectConstructor(() => new StructureMapJobFactory(container.GetInstance<IContext>()));

Upvotes: 0

Views: 103

Answers (1)

Kahbazi
Kahbazi

Reputation: 14995

you can use this :

public static T InstantiateType<T>() where T : new()
{
    return new T();
}

Upvotes: 1

Related Questions