Beispiel #1
0
        public void Intercept(IInvocation invocation)
        {
            PropertyInfo pinfo = invocation.TargetType.GetProperty(invocation.Method.Name.Substring(4));

            ShadowProperty sp = null;

            if ((invocation.Proxy as IDictionary <string, object>).ContainsKey(pinfo.Name))
            {
                sp = (invocation.Proxy as IDictionary <string, object>)[pinfo.Name] as ShadowProperty;
            }
            if (sp == null)
            {
                Type spType = typeof(ShadowCollection <>).MakeGenericType(pinfo.PropertyType.GetGenericArguments());
                sp = (ShadowProperty)Activator.CreateInstance(
                    spType,
                    Context, pinfo, invocation.Proxy as IProxyTargetAccessor);

                foreach (object item in (invocation.Method.Invoke(invocation.InvocationTarget, new object[0]) as IEnumerable) ?? new object[0])
                {
                    IOgmEntity entity;
                    if (item is IProxyTargetAccessor == false)
                    {
                        entity = Context.ObjectWalker.Visit(item as IOgmEntity);
                    }
                    else
                    {
                        entity = item as IOgmEntity;
                    }

                    spType.GetMethod(nameof(ICollection <int> .Add)).Invoke(sp, new[] { entity });
                }
            }

            object target = pinfo.GetValue(invocation.InvocationTarget);

            if (target == null && pinfo.CanWrite)
            {
                pinfo.SetValue(invocation.InvocationTarget, Activator.CreateInstance(pinfo.PropertyType));
            }

            invocation.ReturnValue = Context.ProxyGenerator.CreateInterfaceProxyWithTarget(
                pinfo.PropertyType,
                pinfo.PropertyType.GetInterfaces(),
                target,
                (IInterceptor)Activator.CreateInstance(typeof(CollectionInterceptor <>).MakeGenericType(pinfo.PropertyType.GetGenericArguments()), sp));
        }
Beispiel #2
0
        public void Intercept(IInvocation invocation)
        {
            PropertyInfo pinfo = invocation.TargetType.GetProperty(invocation.Method.Name.Substring(4));
            IEnumerable  arg   = invocation.Arguments[0] as IEnumerable;

            ShadowProperty sp = null;

            if ((invocation.Proxy as IDictionary <string, object>).ContainsKey(pinfo.Name))
            {
                sp = (invocation.Proxy as IDictionary <string, object>)[pinfo.Name] as ShadowProperty;
            }

            Type spType = typeof(ShadowCollection <>).MakeGenericType(pinfo.PropertyType.GetGenericArguments());

            if (sp == null)
            {
                sp = (ShadowProperty)Activator.CreateInstance(
                    spType,
                    Context, pinfo, invocation.Proxy as IProxyTargetAccessor);
            }

            spType.GetMethod(nameof(ICollection <int> .Clear)).Invoke(sp, new object[0]);

            foreach (object item in arg ?? new object[0])
            {
                IOgmEntity entity;
                if (item is IProxyTargetAccessor == false)
                {
                    entity = Context.ObjectWalker.Visit(item as IOgmEntity);
                }
                else
                {
                    entity = item as IOgmEntity;
                }

                spType.GetMethod(nameof(ICollection <int> .Add)).Invoke(sp, new[] { entity });
            }

            invocation.Proceed();
        }