At this point, depending on the platform is available from one to three activators.

  • ReflectionActivator - for creating objects using reflection (Available on all platforms).
  • EmitActivator - for creating objects using emit (Available on all platforms except Windows phone 7).
  • ExpressionActivator - for creating objects using expression trees (Available on all platforms except Windows phone 7 and NET 2).

An example of how to set a specific activator for bindings:

var injector = new MugenInjector();
injector.Bind<Test>().ToSelf().UseExpressionActivator();
injector.Bind<Test>().ToSelf().UseEmitActivator();
injector.Bind<Test>().ToSelf().UseReflectionActivator();
injector.Bind<Test>().ToSelf().UseCustomActivator(/*Your activator here*/);

An example of how to set the default activator for all bindings:

var setting = new DefaultInjectorSetting();
setting.DefaultActivator = typeof (ExpressionActivator);
var injector = new MugenInjector();

You can always create your own activator, for that you can implement an interface IActivator, or inherited from a base class ActivatorBase or existing class which implements an interface test.

An example of how you can change the behavior of an existing class. All properties that we can resolve with the help of the container will be set.

    public class MyExpressionActivator : ExpressionActivator
    {
        /// <summary>
        /// Get property for inject.
        /// </summary>
        /// <param name="service">Specified service type.</param><param name="attributeType">Specified attribute for inject type.</param>
        /// <returns>
        /// Property for inject.
        /// </returns>
        protected override IList<PropertyInfo> GetPropertyForInject(Type service, Type attributeType)
        {
            IList<PropertyInfo> propertyForInject = base.GetPropertyForInject(service, attributeType);
            foreach (PropertyInfo propertyInfo in service.GetProperties(GetBindingFlags()))
            {
                if (CurrentContext.Parameters.Any(c => c.MemberType == MemberTypes.Property && c.Name.Equals(propertyInfo.Name)))
                {
                    propertyForInject.Add(propertyInfo);
                    continue;
                }
                if (!CurrentContext.Injector.CanResolve(propertyInfo.PropertyType, true)) continue;
                propertyForInject.Add(propertyInfo);
            }
            return propertyForInject;
        }
    }

And one more example:

    public class MyActivator : ActivatorBase
    {

        #region Overrides of ActivatorBase

        /// <summary>
        /// Create new instance of specified service.
        /// </summary>
        /// <param name="constructorInfo">Specified <see cref="T:System.Reflection.ConstructorInfo"/>.</param>
        protected override object Activate(ConstructorInfo constructorInfo)
        {
            object[] parameters = InjectorUtils.GetParameters(CurrentContext.Injector, constructorInfo, CurrentContext.Parameters);
            return constructorInfo.Invoke(parameters);
        }

        /// <summary>
        /// Inject property, method, fields.
        /// </summary>
        /// <param name="obj">Specified object.</param>
        protected override void Inject(object obj)
        {
        }

        #endregion
    }

Last edited Apr 4, 2012 at 6:05 PM by VVS0205, version 2

Comments

No comments yet.