Пример #1
0
        /// <summary>
        /// Extension method for the UmbracoObjectTypes enum to return the enum GUID
        /// </summary>
        /// <param name="umbracoObjectType">UmbracoObjectTypes Enum value</param>
        /// <returns>a GUID value of the UmbracoObjectTypes</returns>
        public static Guid GetGuid(this UmbracoObjectTypes umbracoObjectType)
        {
            if (UmbracoObjectTypeCache.ContainsKey(umbracoObjectType))
            {
                return(UmbracoObjectTypeCache[umbracoObjectType]);
            }

            var type       = typeof(UmbracoObjectTypes);
            var memInfo    = type.GetMember(umbracoObjectType.ToString());
            var attributes = memInfo[0].GetCustomAttributes(typeof(UmbracoObjectTypeAttribute),
                                                            false);

            if (attributes.Length == 0)
            {
                return(Guid.Empty);
            }

            var attribute = ((UmbracoObjectTypeAttribute)attributes[0]);

            if (attribute == null)
            {
                return(Guid.Empty);
            }

            UmbracoObjectTypeCache.Add(umbracoObjectType, attribute.ObjectId);

            return(attribute.ObjectId);
        }
Пример #2
0
        /// <summary>
        /// Gets the Type of an entity by its <see cref="UmbracoObjectTypes"/>
        /// </summary>
        /// <param name="umbracoObjectType"><see cref="UmbracoObjectTypes"/></param>
        /// <returns>Type of the entity</returns>
        public virtual Type GetEntityType(UmbracoObjectTypes umbracoObjectType)
        {
            var type       = typeof(UmbracoObjectTypes);
            var memInfo    = type.GetMember(umbracoObjectType.ToString());
            var attributes = memInfo[0].GetCustomAttributes(typeof(UmbracoObjectTypeAttribute),
                                                            false);

            var attribute = ((UmbracoObjectTypeAttribute)attributes[0]);

            if (attribute == null)
            {
                throw new NullReferenceException("The passed in UmbracoObjectType does not contain an UmbracoObjectTypeAttribute, which is used to retrieve the Type.");
            }

            if (attribute.ModelType == null)
            {
                throw new NullReferenceException("The passed in UmbracoObjectType does not contain a Type definition");
            }

            return(attribute.ModelType);
        }
        /// <summary>
        /// Extension method for the UmbracoObejctTypes enum to return the enum friendly name
        /// </summary>
        /// <param name="umbracoObjectType">UmbracoObjectTypes value</param>
        /// <returns>a string of the FriendlyName</returns>
        public static string GetFriendlyName(this UmbracoObjectTypes umbracoObjectType)
        {
            var type       = typeof(UmbracoObjectTypes);
            var memInfo    = type.GetMember(umbracoObjectType.ToString());
            var attributes = memInfo[0].GetCustomAttributes(typeof(FriendlyNameAttribute),
                                                            false);

            if (attributes.Length == 0)
            {
                return(string.Empty);
            }

            var attribute = ((FriendlyNameAttribute)attributes[0]);

            if (attribute == null)
            {
                return(string.Empty);
            }

            return(attribute.ToString());
        }
        /// <summary>
        /// Extension method for the UmbracoObjectTypes enum to return the enum GUID
        /// </summary>
        /// <param name="umbracoObjectType">UmbracoObjectTypes Enum value</param>
        /// <returns>a GUID value of the UmbracoObjectTypes</returns>
        public static Guid GetGuid(this UmbracoObjectTypes umbracoObjectType)
        {
            return(UmbracoObjectTypeCache.GetOrAdd(umbracoObjectType, types =>
            {
                var type = typeof(UmbracoObjectTypes);
                var memberInfo = type.GetMember(umbracoObjectType.ToString());
                var attributes = memberInfo[0].GetCustomAttributes(typeof(UmbracoObjectTypeAttribute), false);

                if (attributes.Length == 0)
                {
                    return Guid.Empty;
                }

                var attribute = (UmbracoObjectTypeAttribute)attributes[0];
                if (attribute == null)
                {
                    return Guid.Empty;
                }

                return attribute.ObjectId;
            }));
        }
        public static string GetUdiType(this UmbracoObjectTypes umbracoObjectType)
        {
            return(UmbracoObjectTypeUdiCache.GetOrAdd(umbracoObjectType, types =>
            {
                var type = typeof(UmbracoObjectTypes);
                var memInfo = type.GetMember(umbracoObjectType.ToString());
                var attributes = memInfo[0].GetCustomAttributes(typeof(UmbracoUdiTypeAttribute),
                                                                false);

                if (attributes.Length == 0)
                {
                    return Constants.UdiEntityType.Unknown;
                }

                var attribute = ((UmbracoUdiTypeAttribute)attributes[0]);
                if (attribute == null)
                {
                    return Constants.UdiEntityType.Unknown;
                }

                return attribute.UdiType;
            }));
        }