Example #1
0
        private PropertyDescriptorCollection GetPropertiesWithMetadata(PropertyDescriptorCollection originalCollection)
        {
            if (AssociatedMetadataType == null)
            {
                return(originalCollection);
            }

            bool customDescriptorsCreated = false;
            List <PropertyDescriptor> tempPropertyDescriptors = new List <PropertyDescriptor>();

            foreach (PropertyDescriptor propDescriptor in originalCollection)
            {
                Attribute[]        newMetadata = TypeDescriptorCache.GetAssociatedMetadata(AssociatedMetadataType, propDescriptor.Name);
                PropertyDescriptor descriptor  = propDescriptor;
                if (newMetadata.Length > 0)
                {
                    // Create a metadata descriptor that wraps the property descriptor
                    descriptor = new MetadataPropertyDescriptorWrapper(propDescriptor, newMetadata);
                    customDescriptorsCreated = true;
                }

                tempPropertyDescriptors.Add(descriptor);
            }

            if (customDescriptorsCreated)
            {
                return(new PropertyDescriptorCollection(tempPropertyDescriptors.ToArray(), true));
            }
            return(originalCollection);
        }
Example #2
0
        public static Accessor FindPropertyByName <TEntity>(string propertyName)
        {
            var propertyToFind = propertyName;
            var prefix         = typeof(TEntity).Name;

            if (propertyToFind.StartsWith(prefix))
            {
                propertyToFind = propertyToFind.Substring(prefix.Length);
            }

            //This is to handle the situation with address where we have properties that start with the type name.
            //We won't find any properties for 1 on address when we are looking for address1 and then rip off the type name.
            var property = TypeDescriptorCache.GetPropertyFor(typeof(TEntity), propertyToFind) ??
                           TypeDescriptorCache.GetPropertyFor(typeof(TEntity), propertyName);

            if (property != null)
            {
                return(new SingleProperty(property));
            }

            if (ExtensionProperties.HasExtensionFor(typeof(TEntity)))
            {
                var extensionType         = ExtensionProperties.ExtensionFor(typeof(TEntity));
                var extensionProperty     = TypeDescriptorCache.GetPropertyFor(extensionType, propertyToFind);
                var extensionAccessorType = typeof(ExtensionPropertyAccessor <>).MakeGenericType(extensionType);
                return((Accessor)Activator.CreateInstance(extensionAccessorType, extensionProperty));
            }

            return(null);
        }
 public AssociatedMetadataTypeTypeDescriptor(ICustomTypeDescriptor parent, Type type, Type associatedMetadataType)
     : base(parent)
 {
     AssociatedMetadataType = associatedMetadataType ?? TypeDescriptorCache.GetAssociatedMetadataType(type);
     if (AssociatedMetadataType != null)
     {
         TypeDescriptorCache.ValidateMetadataType(type, AssociatedMetadataType);
     }
 }
 public GeneratedMetadataTypeDescriptor(ICustomTypeDescriptor parent, Type type)
     : base(parent)
 {
     this.generatedMetadataType = TypeDescriptorCache.GetGeneratedMetadataType(type);
     if (this.generatedMetadataType != null)
     {
         TypeDescriptorCache.ValidateMetadataType(type, this.generatedMetadataType);
     }
 }
Example #5
0
 public AssociatedMetadataTypeTypeDescriptor(
     ICustomTypeDescriptor parent,
     [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type,
     [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type associatedMetadataType)
     : base(parent)
 {
     AssociatedMetadataType = associatedMetadataType ?? TypeDescriptorCache.GetAssociatedMetadataType(type);
     IsSelfAssociated       = (type == AssociatedMetadataType);
     if (AssociatedMetadataType != null)
     {
         TypeDescriptorCache.ValidateMetadataType(type, AssociatedMetadataType);
     }
 }
Example #6
0
        public static FubuMVC.Swank.Specification.Specification GetSpec(Action <Swank> configure = null)
        {
            var graph            = Behavior.BuildGraph().AddActionsInThisNamespace();
            var moduleConvention = new ModuleConvention(new MarkerConvention <ModuleDescription>());

            var resourceConvention = new ResourceConvention(
                new MarkerConvention <ResourceDescription>(),
                new BehaviorSource(graph, Swank.CreateConfig(x => x.AppliesToThisAssembly().Where(ActionFilter))));

            var configuration = Swank.CreateConfig(x =>
            {
                x.AppliesToThisAssembly().Where(ActionFilter).WithEnumFormat(EnumFormat.AsString);
                if (configure != null)
                {
                    configure(x);
                }
            });
            var typeCache        = new TypeDescriptorCache();
            var memberConvention = new MemberConvention();
            var optionFactory    = new OptionFactory(configuration,
                                                     new EnumConvention(), new OptionConvention());
            var specBuilder = new FubuMVC.Swank.Specification.SpecificationService(
                configuration,
                new BehaviorSource(graph, configuration),
                typeCache,
                moduleConvention,
                resourceConvention,
                new EndpointConvention(),
                memberConvention,
                new StatusCodeConvention(),
                new HeaderConvention(),
                new MimeTypeConvention(),
                new TypeGraphFactory(
                    configuration,
                    typeCache,
                    new TypeConvention(configuration),
                    memberConvention,
                    optionFactory),
                new BodyDescriptionFactory(configuration),
                new OptionFactory(configuration,
                                  new EnumConvention(),
                                  new OptionConvention()));

            return(specBuilder.Generate());
        }
Example #7
0
        /// <exception cref="ArgumentNullException"><paramref name="type"/> is <see langword="null"/></exception>
        /// <exception cref="TypeLoadException">A custom attribute type could not be loaded.</exception>
        /// <exception cref="InvalidCastException">An element in the sequence cannot be cast to type <see cref="Attribute"/>.</exception>
        public TypeDescriptor(Type type)
        {
            Guard.IsNotNull(type, nameof(type));

            Type = type;
            if (Cache.TryGetValue(Type, out var cache))
            {
                Info = cache;
            }
            else
            {
                lock (Cache)
                {
                    if (Cache.TryGetValue(Type, out cache))
                    {
                        Info = cache;
                    }
                    else
                    {
                        Info = new TypeDescriptorCache
                        {
                            Properties = Type.GetProperties().Select(p => new PropertyDescriptor(p, this)).ToImmutableArray(),
                            Fields     = Type.GetFields().Select(f => new FieldDescriptor(f, this)).ToImmutableArray()
                        };

                        Cache.Add(Type, Info);
                    }
                }
            }

            if (AttributeCache.TryGetValue(Type, out var attributeCache))
            {
                Attributes = attributeCache;
            }
            else
            {
                lock (AttributeCache)
                {
                    if (AttributeCache.TryGetValue(Type, out attributeCache))
                    {
                        Attributes = attributeCache;
                    }
                    else
                    {
                        var implemented = Type.GetCustomAttributes(false).Cast <Attribute>().Select(static a => new AttributeDescriptorCache(a, false)).ToList();
Example #8
0
        public ObjectBlockSettings()
        {
            _cache = new TypeDescriptorCache();

            _ignored = new List <IIgnoreAccessorPolicy>();

            _byAccessor = new Cache <Accessor, CollectionConfiguration>(
                x => x.InnerProperty.HasAttribute <BlockSettingsAttribute>()
                    ? x.ToCollectionConfiguration()
                    : new CollectionConfiguration(x));

            _collectionsFor = new Cache <Type, IList <CollectionConfiguration> >(
                type => _cache.GetPropertiesFor(type)
                .Values
                .Where(x => x.PropertyType.IsGenericEnumerable())
                .Select(x => _byAccessor[new SingleProperty(x)])
                .ToList());
        }
Example #9
0
        protected FubuMVC.Swank.Specification.Specification BuildSpec <TNamespace>(BehaviorGraph graph, Action <Swank> configure = null)
        {
            var configuration = Swank.CreateConfig(x =>
            {
                if (configure != null)
                {
                    configure(x);
                }

                x.AppliesToThisAssembly()
                .Where(y => y.FirstCall().HandlerType.InNamespace <TNamespace>());
            });

            var behaviorSource     = new BehaviorSource(graph, configuration);
            var resourceConvention = new ResourceConvention(new MarkerConvention <ResourceDescription>(), behaviorSource);
            var moduleConvention   = new ModuleConvention(new MarkerConvention <ModuleDescription>());
            var typeCache          = new TypeDescriptorCache();
            var memberConvention   = new MemberConvention();
            var optionFactory      = new OptionFactory(configuration,
                                                       new EnumConvention(), new OptionConvention());

            return(new FubuMVC.Swank.Specification.SpecificationService(
                       configuration,
                       new BehaviorSource(graph, configuration),
                       typeCache,
                       moduleConvention,
                       resourceConvention,
                       new EndpointConvention(),
                       memberConvention,
                       new StatusCodeConvention(),
                       new HeaderConvention(),
                       new MimeTypeConvention(),
                       new TypeGraphFactory(
                           configuration,
                           typeCache,
                           new TypeConvention(configuration),
                           memberConvention,
                           optionFactory),
                       new BodyDescriptionFactory(configuration),
                       new OptionFactory(configuration,
                                         new EnumConvention(),
                                         new OptionConvention())).Generate());
        }
Example #10
0
        public static FubuMVC.Swank.Specification.Specification GetSpec(Action<Swank> configure = null)
        {
            var graph = Behavior.BuildGraph().AddActionsInThisNamespace();
            var moduleConvention = new ModuleConvention(new MarkerConvention<ModuleDescription>());

            var resourceConvention = new ResourceConvention(
                new MarkerConvention<ResourceDescription>(),
                new BehaviorSource(graph, Swank.CreateConfig(x => x.AppliesToThisAssembly().Where(ActionFilter))));

            var configuration = Swank.CreateConfig(x =>
                {
                    x.AppliesToThisAssembly().Where(ActionFilter).WithEnumFormat(EnumFormat.AsString);
                    if (configure != null) configure(x);
                });
            var typeCache = new TypeDescriptorCache();
            var memberConvention = new MemberConvention();
            var optionFactory = new OptionFactory(configuration,
                new EnumConvention(), new OptionConvention());
            var specBuilder = new FubuMVC.Swank.Specification.SpecificationService(
                configuration,
                new BehaviorSource(graph, configuration),
                typeCache,
                moduleConvention,
                resourceConvention,
                new EndpointConvention(),
                memberConvention,
                new StatusCodeConvention(),
                new HeaderConvention(),
                new MimeTypeConvention(),
                new TypeGraphFactory(
                    configuration, 
                    typeCache,
                    new TypeConvention(configuration), 
                    memberConvention,
                    optionFactory), 
                new BodyDescriptionFactory(configuration),
                new OptionFactory(configuration,
                    new EnumConvention(), 
                    new OptionConvention()));
            return specBuilder.Generate();
        }
 private PropertyDescriptorCollection GetPropertiesWithMetadata(PropertyDescriptorCollection originalCollection)
 {
     if (this.generatedMetadataType != null)
     {
         bool flag = false;
         List <PropertyDescriptor> list = new List <PropertyDescriptor>();
         foreach (PropertyDescriptor descriptor in originalCollection)
         {
             Attribute[]        generatedMetadata = TypeDescriptorCache.GetGeneratedMetadata(this.generatedMetadataType, descriptor.Name);
             PropertyDescriptor item = descriptor;
             if (generatedMetadata.Length > 0)
             {
                 item = new GeneratedMetadataPropertyDescriptor(descriptor, generatedMetadata);
                 flag = true;
             }
             list.Add(item);
         }
         if (flag)
         {
             return(new PropertyDescriptorCollection(list.ToArray(), true));
         }
     }
     return(originalCollection);
 }
Example #12
0
 public void SetUp()
 {
     _cache = new TypeDescriptorCache();
 }
Example #13
0
 public RecipeWriter(TypeDescriptorCache types)
 {
     _types = types;
 }
 public void SetUp()
 {
     _cache = new TypeDescriptorCache();
 }
Example #15
0
 public RecipeWriter(TypeDescriptorCache types)
 {
     _types = types;
 }
Example #16
0
 static BlockSettingsAttribute()
 {
     Cache = new TypeDescriptorCache();
 }