示例#1
0
        private static readonly int AccessorPrefixLength = "?et_".Length;         // get_ or set_

        public static bool Handle(Invocation invocation, Mock mock)
        {
            if (mock.AutoSetupPropertiesDefaultValueProvider == null)
            {
                return(false);
            }

            MethodInfo invocationMethod = invocation.Method;

            if (invocationMethod.IsPropertyAccessor() && !invocationMethod.IsPropertyIndexerAccessor())
            {
                string       propertyNameToSearch = invocationMethod.Name.Substring(AccessorPrefixLength);
                PropertyInfo property             = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch);

                if (property == null)
                {
                    return(false);
                }

                var expression = GetPropertyExpression(invocationMethod.DeclaringType, property);

                object propertyValue;

                Setup getterSetup = null;
                if (property.CanRead)
                {
                    var getter = property.GetGetMethod(true);
                    propertyValue = CreateInitialPropertyValue(mock, getter);
                    getterSetup   = new AutoImplementedPropertyGetterSetup(expression, getter, () => propertyValue);
                    mock.Setups.Add(getterSetup);
                }

                Setup setterSetup = null;
                if (property.CanWrite)
                {
                    MethodInfo setter = property.GetSetMethod(nonPublic: true);
                    setterSetup = new AutoImplementedPropertySetterSetup(expression, setter, (newValue) =>
                    {
                        propertyValue = newValue;
                    });
                    mock.Setups.Add(setterSetup);
                }

                Setup setupToExecute = invocationMethod.IsPropertyGetter() ? getterSetup : setterSetup;
                setupToExecute.Execute(invocation);

                return(true);
            }
            else
            {
                return(false);
            }
        }
        private static readonly int AccessorPrefixLength = "?et_".Length;         // get_ or set_

        public static bool Handle(Invocation invocation, Mock mock)
        {
            if (mock.AutoSetupPropertiesDefaultValueProvider == null)
            {
                return(false);
            }

            MethodInfo invocationMethod = invocation.Method;

            if (invocationMethod.IsPropertyAccessor())
            {
                string       propertyNameToSearch = invocationMethod.Name.Substring(AccessorPrefixLength);
                PropertyInfo property             = invocationMethod.DeclaringType.GetProperty(propertyNameToSearch);

                if (property == null)
                {
                    return(false);
                }

                var expression = GetPropertyExpression(invocationMethod.DeclaringType, property);

                object propertyValue;

                Setup getterSetup = null;
                if (property.CanRead(out var getter))
                {
                    if (ProxyFactory.Instance.IsMethodVisible(getter, out _))
                    {
                        propertyValue = CreateInitialPropertyValue(mock, getter);
                        getterSetup   = new AutoImplementedPropertyGetterSetup(mock, expression, getter, () => propertyValue);
                        mock.MutableSetups.Add(getterSetup);
                    }

                    // If we wanted to optimise for speed, we'd probably be forgiven
                    // for removing the above `IsMethodVisible` guard, as it's rather
                    // unlikely to encounter non-public getters such as the following
                    // in real-world code:
                    //
                    //     public T Property { internal get; set; }
                    //
                    // Usually, it's only the setters that are made non-public. For
                    // now however, we prefer correctness.
                }

                Setup setterSetup = null;
                if (property.CanWrite(out var setter))
                {
                    if (ProxyFactory.Instance.IsMethodVisible(setter, out _))
                    {
                        setterSetup = new AutoImplementedPropertySetterSetup(mock, expression, setter, (newValue) =>
                        {
                            propertyValue = newValue;
                        });
                        mock.MutableSetups.Add(setterSetup);
                    }
                }

                Setup setupToExecute = invocationMethod.IsGetAccessor() ? getterSetup : setterSetup;
                setupToExecute.Execute(invocation);

                return(true);
            }
            else
            {
                return(false);
            }
        }