public ICollection <DiscoveredResource> GetResources(Type target, string resourceKeyPrefix)
        {
            var enumType = Enum.GetUnderlyingType(target);
            var isHidden = target.GetCustomAttribute <HiddenAttribute>() != null;

            string GetEnumTranslation(MemberInfo mi)
            {
                var result           = mi.Name;
                var displayAttribute = mi.GetCustomAttribute <DisplayAttribute>();

                if (displayAttribute != null)
                {
                    result = displayAttribute.Name;
                }

                return(result);
            }

            return(target.GetMembers(BindingFlags.Public | BindingFlags.Static)
                   .Select(mi =>
            {
                var isResourceHidden = isHidden || mi.GetCustomAttribute <HiddenAttribute>() != null;

                var translations = DiscoveredTranslation.FromSingle(GetEnumTranslation(mi));
                var additionalTranslationsAttributes =
                    mi.GetCustomAttributes <TranslationForCultureAttribute>().ToList();
                if (additionalTranslationsAttributes.Any())
                {
                    translations.AddRange(additionalTranslationsAttributes.Select(_ =>
                                                                                  new DiscoveredTranslation(_.Translation, _.Culture)));
                }

                return new DiscoveredResource(mi,
                                              ResourceKeyBuilder.BuildResourceKey(target, mi.Name),
                                              translations,
                                              mi.Name,
                                              target,
                                              enumType,
                                              enumType.IsSimpleType(),
                                              isResourceHidden);
            })
                   .ToList());
        }
Exemplo n.º 2
0
        public ICollection <DiscoveredResource> GetClassLevelResources(Type target, string resourceKeyPrefix)
        {
            var result = new List <DiscoveredResource>();
            var resourceAttributesOnModelClass = target.GetCustomAttributes <ResourceKeyAttribute>().ToList();

            if (!resourceAttributesOnModelClass.Any())
            {
                return(result);
            }

            foreach (var resourceKeyAttribute in resourceAttributesOnModelClass)
            {
                result.Add(new DiscoveredResource(null,
                                                  ResourceKeyBuilder.BuildResourceKey(resourceKeyPrefix, resourceKeyAttribute.Key, string.Empty),
                                                  DiscoveredTranslation.FromSingle(resourceKeyAttribute.Value),
                                                  resourceKeyAttribute.Value,
                                                  target,
                                                  typeof(string),
                                                  true));
            }

            return(result);
        }