internal static PropertyInfo ResolveProperty(Type type, string name, bool ignoreCase, object[] indexArgs, bool hasInstance, object setterValue = null, bool getter = true)
        {
            var candidates = type.GetAllProperties().Where(prop => MockingUtil.StringEqual(prop.Name, name, ignoreCase)).ToArray();

            if (candidates.Length == 1)
            {
                return(candidates[0]);
            }

            if (!getter)
            {
                Array.Resize(ref indexArgs, indexArgs.Length + 1);
                indexArgs[indexArgs.Length - 1] = setterValue;
            }

            var propMethods = candidates
                              .Select(prop => getter ? prop.GetGetMethod(true) : prop.GetSetMethod(true))
                              .Where(m => m != null && CanCall(m, hasInstance))
                              .ToArray();

            indexArgs = indexArgs ?? MockingUtil.NoObjects;
            object state;
            var    foundGetter = MockingUtil.BindToMethod(MockingUtil.AllMembers, propMethods, ref indexArgs, null, null, null, out state);

            return(candidates.First(prop => (getter ? prop.GetGetMethod(true) : prop.GetSetMethod(true)) == foundGetter));
        }
Esempio n. 2
0
        public ref TRefReturn CallMethod <TRefReturn>(string name, params object[] args)
        {
            return(ref ProfilerInterceptor.GuardInternal((target, arguments) =>
            {
                arguments = arguments ?? MockingUtil.NoObjects;
                var candidates = type.GetAllMethods()
                                 .Where(m => m.Name == name && MockingUtil.CanCall(m, this.instance != null))
                                 .Select(m => MockingUtil.TrySpecializeGenericMethod(m, arguments.Select(a => a != null ? a.GetType() : null).ToArray()) ?? m)
                                 .ToArray();
                object state;
                var method = MockingUtil.BindToMethod(MockingUtil.AllMembers, candidates, ref arguments, null, null, null, out state);

                ProfilerInterceptor.RefReturn <TRefReturn> @delegate =
                    MockingUtil.CreateDynamicMethodInvoker <TRefReturn>(target, method as MethodInfo, arguments);

                return ref ProfilerInterceptor.GuardExternal(@delegate, target, arguments);
            }, this.instance, args ?? MockingUtil.NoObjects));
        }
Esempio n. 3
0
        public static object CreateObject(this Type type, object[] args)
        {
            args = args ?? NoObjects;

            var constructorInfos = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            if (constructorInfos.Length == 0 || (type.IsValueType && args.Length == 0))
            {
                if (args.Length > 0)
                {
                    throw new MockException("Type has no non-default constructors.");
                }

                return(type.GetDefaultValue());
            }

            object state;
            var    ctor = (ConstructorInfo)MockingUtil.BindToMethod(MockingUtil.Default,
                                                                    constructorInfos, ref args, null, null, null, out state);

            var ctorParameters = ctor.GetParameters();

            for (int i = 0; i < ctorParameters.Length; ++i)
            {
                var paramType = ctorParameters[i].ParameterType;
                if (paramType.IsValueType && args[i] == null)
                {
                    args[i] = paramType.GetDefaultValue();
                }
                else if (args[i] != null && !paramType.IsAssignableFrom(args[i].GetType()))
                {
                    args[i] = Convert.ChangeType(args[i], paramType, System.Globalization.CultureInfo.CurrentCulture);
                }
            }

#if !PORTABLE
            var newCall = MockingUtil.CreateDynamicMethod <Func <object[], object> >(il =>
            {
                il.UnpackArgArray(OpCodes.Ldarg_0, ctor);
                il.PushArgArray(ctor);
                il.Emit(OpCodes.Newobj, ctor);
                if (type.IsValueType)
                {
                    il.Emit(OpCodes.Box, type);
                }
                il.Emit(OpCodes.Ret);
            });

            return(ProfilerInterceptor.GuardExternal(() =>
            {
                try
                {
                    return newCall(args);
                }
                catch (MemberAccessException ex)
                {
                    GC.KeepAlive(ex);
                    return MockingUtil.CreateInstance(type, args);
                }
            }));
#else
            return(MockingUtil.CreateInstance(type, args));
#endif
        }