Cannot inject private set() methods of derived properties


While I am commenting and my memory functions, while working on the code fork I also stumbled on this one:
class B {}

class A1 {
  public B BProp { get; private set; }

class A2 : A1 {}

Injector.SetResolver<A2>().AddPropertyInjector((a) => a.BProp);
The above code will error out with some security violations. I don't have the original wiki solving this handy, but it can be solved by evaluating the expression against the parent type.
            private MemberExpression GetSetterExpressionBody(ParameterExpression instanceVar, SetterExpression expr) {
                var member = expr.PropertyMemberExpression.Member;
                if (member is PropertyInfo) {
                    // Trick to handle Property{get; private set} where the property is declared on a parent type
                    var propMem = (PropertyInfo)member;
                    if (propMem.DeclaringType != typeofT) {
                        propMem = propMem.DeclaringType
                            .GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                            .Where (p => p.Name.Equals(propMem.Name))
                            .First ();

                    return Expression.Property (instanceVar, propMem);
                } else {
                    return Expression.Field(instanceVar, (FieldInfo) member);


Grax wrote Jul 18, 2013 at 5:32 PM

I didn't get an error when I tried your sample code. Everything "just worked." The only thing I changed was
Injector.SetResolver<A2>(() => new A2() )
.AddPropertyInjector((a) => a.BProp);
since it would not compile without an expression in the factoryExpression parameter of SetResolver .

If you have the exact error message, I would appreciate that so I can attempt to duplicate the issue.

iamahern wrote Jul 23, 2013 at 8:47 AM

Sorry about this - the issue turned out to be an artifact of an extension I implemented. The only way to reproduce the issue is to use reflection:

var props = typeof(SomeType).getProperties()...;

When you use reflection as above, the PropertyInfo returned has the issue I described.