VPN has been down today for me... so you're in luck, assuming I understand your request:
public class MemberInjectionModule : Module
{
protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
{
registration.Activated += OnComponentActivated;
}
private void OnComponentActivated(object sender, ActivatedEventArgs<object> e)
{
const BindingFlags flags = BindingFlags.Instance |
BindingFlags.NonPublic |
BindingFlags.Public;
var type = e.Instance.GetType();
foreach (var field in type.GetFields(flags))
{
if (field.GetCustomAttributes(typeof(InjectedAttribute), true).Length > 0)
field.SetValue(e.Instance, e.Context.Resolve(field.FieldType));
}
foreach (var prop in type.GetProperties(flags))
{
if (prop.GetCustomAttributes(typeof(InjectedAttribute), true).Length > 0)
{
if (prop.GetIndexParameters().Length > 0)
throw new InvalidOperationException("Indexed properties cannot be injected.");
prop.SetValue(e.Instance, e.Context.Resolve(prop.PropertyType), null);
}
}
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class InjectedAttribute : Attribute { }
public class SampleClass
{
[Injected]
public IOther InjectedOther { get; set; }
public IOther NotInjectedOther { get; set; }
}
[Test]
public void MemberInjectionModule_checks_InjectedAttribute_and_injects_dependency()
{
var builder = new ContainerBuilder();
builder.RegisterModule<MemberInjectionModule>();
builder.RegisterType<Other1>().As<IOther>();
builder.RegisterType<SampleClass>();
var container = builder.Build();
var resolved = container.Resolve<SampleClass>();
Assert.That(resolved.InjectedOther, Is.Not.Null);
Assert.That(resolved.NotInjectedOther, Is.Null);
}