public void SetNavigationPropertyValue(RelatedEnd relatedEnd, object value)
 {
     if (relatedEnd == null)
     {
         return;
     }
     if (relatedEnd.TargetAccessor.ValueSetter == null)
     {
         Type         declaringType = PocoPropertyAccessorStrategy.GetDeclaringType(relatedEnd);
         PropertyInfo topProperty   = declaringType.GetTopProperty(relatedEnd.TargetAccessor.PropertyName);
         if (topProperty == (PropertyInfo)null)
         {
             throw new EntityException(Strings.PocoEntityWrapper_UnableToSetFieldOrProperty((object)relatedEnd.TargetAccessor.PropertyName, (object)declaringType.FullName));
         }
         EntityProxyFactory entityProxyFactory = new EntityProxyFactory();
         relatedEnd.TargetAccessor.ValueSetter = entityProxyFactory.CreateBaseSetter(topProperty.DeclaringType, topProperty);
     }
     try
     {
         relatedEnd.TargetAccessor.ValueSetter(this._entity, value);
     }
     catch (Exception ex)
     {
         throw new EntityException(Strings.PocoEntityWrapper_UnableToSetFieldOrProperty((object)relatedEnd.TargetAccessor.PropertyName, (object)this._entity.GetType().FullName), ex);
     }
 }
        public object GetNavigationPropertyValue(RelatedEnd relatedEnd)
        {
            object obj = (object)null;

            if (relatedEnd != null)
            {
                if (relatedEnd.TargetAccessor.ValueGetter == null)
                {
                    Type         declaringType = PocoPropertyAccessorStrategy.GetDeclaringType(relatedEnd);
                    PropertyInfo topProperty   = declaringType.GetTopProperty(relatedEnd.TargetAccessor.PropertyName);
                    if (topProperty == (PropertyInfo)null)
                    {
                        throw new EntityException(Strings.PocoEntityWrapper_UnableToSetFieldOrProperty((object)relatedEnd.TargetAccessor.PropertyName, (object)declaringType.FullName));
                    }
                    EntityProxyFactory entityProxyFactory = new EntityProxyFactory();
                    relatedEnd.TargetAccessor.ValueGetter = entityProxyFactory.CreateBaseGetter(topProperty.DeclaringType, topProperty);
                }
                bool state = relatedEnd.DisableLazyLoading();
                try
                {
                    obj = relatedEnd.TargetAccessor.ValueGetter(this._entity);
                }
                catch (Exception ex)
                {
                    throw new EntityException(Strings.PocoEntityWrapper_UnableToSetFieldOrProperty((object)relatedEnd.TargetAccessor.PropertyName, (object)this._entity.GetType().FullName), ex);
                }
                finally
                {
                    relatedEnd.ResetLazyLoading(state);
                }
            }
            return(obj);
        }
        public bool CollectionRemove(RelatedEnd relatedEnd, object value)
        {
            object entity = this._entity;

            try
            {
                object navigationPropertyValue = this.GetNavigationPropertyValue(relatedEnd);
                if (navigationPropertyValue != null)
                {
                    if (object.ReferenceEquals(navigationPropertyValue, (object)relatedEnd))
                    {
                        return(true);
                    }
                    if (relatedEnd.TargetAccessor.CollectionRemove == null)
                    {
                        relatedEnd.TargetAccessor.CollectionRemove = PocoPropertyAccessorStrategy.CreateCollectionRemoveFunction(PocoPropertyAccessorStrategy.GetDeclaringType(relatedEnd), relatedEnd.TargetAccessor.PropertyName);
                    }
                    return(relatedEnd.TargetAccessor.CollectionRemove(navigationPropertyValue, value));
                }
            }
            catch (Exception ex)
            {
                throw new EntityException(Strings.PocoEntityWrapper_UnableToSetFieldOrProperty((object)relatedEnd.TargetAccessor.PropertyName, (object)entity.GetType().FullName), ex);
            }
            return(false);
        }
        private static Func <object, object, bool> CreateCollectionRemoveFunction(
            Type type,
            string propertyName)
        {
            Type collectionElementType = EntityUtil.GetCollectionElementType(PocoPropertyAccessorStrategy.GetNavigationPropertyType(type, propertyName));

            return((Func <object, object, bool>)PocoPropertyAccessorStrategy.RemoveFromCollectionGeneric.MakeGenericMethod(collectionElementType).Invoke((object)null, (object[])null));
        }
 public object CollectionCreate(RelatedEnd relatedEnd)
 {
     if (this._entity is IEntityWithRelationships)
     {
         return((object)relatedEnd);
     }
     if (relatedEnd.TargetAccessor.CollectionCreate == null)
     {
         Type   declaringType          = PocoPropertyAccessorStrategy.GetDeclaringType(relatedEnd);
         string propertyName           = relatedEnd.TargetAccessor.PropertyName;
         Type   navigationPropertyType = PocoPropertyAccessorStrategy.GetNavigationPropertyType(declaringType, propertyName);
         relatedEnd.TargetAccessor.CollectionCreate = PocoPropertyAccessorStrategy.CreateCollectionCreateDelegate(navigationPropertyType, propertyName);
     }
     return(relatedEnd.TargetAccessor.CollectionCreate());
 }