Esempio n. 1
0
        protected virtual TPropType OnGet <TOwner, TPropType>(PropertyWrapper <TOwner, TPropType> property)
        {
            object value;

            if (!this.propMap.TryGetValue(property.Name, out value))
            {
                var propertyType = property.PropertyInfo.PropertyType;

                if (propertyType.IsGenericInstanceOf(typeof(IDictionary <,>)))
                {
                    var newDictType =
                        typeof(PostResourceDictionary <,>).MakeGenericType(propertyType.GetGenericArguments());
                    var newDict = Activator.CreateInstance(newDictType,
                                                           BindingFlags.Instance | BindingFlags.NonPublic |
                                                           BindingFlags.CreateInstance, null,
                                                           new object[] { this, property.Name }, null);
                    this.propMap[property.Name] = newDict;
                    return((TPropType)newDict);
                }
                if (propertyType.IsGenericInstanceOf(typeof(ISet <>)))
                {
                    var newSetType = typeof(PostResourceSet <>).MakeGenericType(propertyType.GetGenericArguments());
                    var newSet     = Activator.CreateInstance(newSetType,
                                                              BindingFlags.Instance | BindingFlags.NonPublic |
                                                              BindingFlags.CreateInstance, null,
                                                              new object[] { this, property.Name }, null);
                    this.propMap[property.Name] = newSet;
                    return((TPropType)newSet);
                }
                if (propertyType.IsGenericInstanceOf(typeof(ICollection <>), typeof(IList <>)))
                {
                    var newListType = typeof(PostResourceList <>).MakeGenericType(propertyType.GetGenericArguments());
                    var newList     = Activator.CreateInstance(newListType,
                                                               BindingFlags.Instance | BindingFlags.NonPublic |
                                                               BindingFlags.CreateInstance, null,
                                                               new object[] { this, property.Name }, null);
                    this.propMap[property.Name] = newList;
                    return((TPropType)newList);
                }
                if (typeof(IClientResource).IsAssignableFrom(propertyType))
                {
                    var resourceInfo =
                        propertyType.GetCustomAttributes(typeof(ResourceInfoAttribute), false)
                        .OfType <ResourceInfoAttribute>()
                        .FirstOrDefault();

                    if (resourceInfo != null && resourceInfo.IsValueObject)
                    {
                        var valueObjectForm = Activator.CreateInstance(resourceInfo.PostFormType);
                        this.propMap[property.Name]  = valueObjectForm;
                        this.dirtyMap[property.Name] = true;
                        return((TPropType)valueObjectForm);
                    }
                }
                return(default(TPropType));
            }

            return((TPropType)value);
        }
Esempio n. 2
0
        protected TPropType OnGet <TOwner, TPropType>(PropertyWrapper <TOwner, TPropType> property)
        {
            if (Client == null)
            {
                throw new InvalidOperationException(
                          $"{this}.Initialize(IResourceFetchContext) must be invoked before OnGet.");
            }

            try
            {
                Fetch();
            }
            catch (LoadException exception)
            {
                var resourcePath = this.expandPath ?? property.ToString();
                throw new LazyLoadingDisabledException(resourcePath, exception);
            }

            return(property.Getter((TOwner)ProxyTarget));
        }
Esempio n. 3
0
 protected void OnSet <TOwner, TPropType>(PropertyWrapper <TOwner, TPropType> property, TPropType value)
 {
     throw new InvalidOperationException($"{property} is just a proxy. Use Patch to modify a resource.");
 }
Esempio n. 4
0
 protected virtual void OnSet <TOwner, TPropType>(PropertyWrapper <TOwner, TPropType> property, TPropType value)
 {
     this.propMap[property.Name]  = value;
     this.dirtyMap[property.Name] = true;
 }