Closed
Description
Hi all,
Is there a particular reason that the method decorator doesn't support the instance
variable being populated in the init
method with a reference to this
? As far as I'm aware the IL should support ldarg0
pushing a reference to the object onto the stack.
The reason I ask is because adding this would allow for the decorators to reflectively initialize components of the object prior to the constructor taking place. Examples might include scanning the object for fields or properties with an attribute for dependency injection support, or scanning for methods with an attribute indicating that they should be run when construction has completed.
Here's an example usage:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class InjectAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructAttribute : Attribute, IMethodDecorator
{
private object _instance;
private Type _type;
public void Init(object instance, MethodBase method, object[] args) {
_instance = instance;
_type = method.DeclaringType;
}
public void OnEntry() {
var fields = _type.GetFields().Where(field => field.GetCustomAttribute<InjectAttribute>() != null);
// iterate through, resolve the service reference and assign using:
// field.SetValue(_instance, value);
}
// OnExit, OnException
}
Thanks
Metadata
Metadata
Assignees
Labels
No labels