internal static string TitleOrToString(object obj, string format)
        {
            Type type = obj.GetType();

            lock (titleFrom) {
                if (titleFrom.ContainsKey(type) && titleFrom[type] != null)
                {
                    return(titleFrom[type].GetTitle(obj, format));
                }

                titleFrom[type] = null;

                MethodInfo titleMethod = type.GetMethod("Title", Type.EmptyTypes);
                if (titleMethod != null)
                {
                    titleFrom[type] = new TitleFromTitleMethod(titleMethod);
                }

                if (titleFrom[type] == null)
                {
                    PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (PropertyInfo property in properties)
                    {
                        if (Attribute.GetCustomAttribute(property, typeof(TitleAttribute)) != null)
                        {
                            titleFrom[type] = new TitleFromProperty(property);
                        }
                    }
                }

                if (titleFrom[type] == null && format != null)
                {
                    MethodInfo formatMethod = type.GetMethod("ToString", new[] { typeof(string) });
                    if (formatMethod != null)
                    {
                        titleFrom[type] = new TitleFromFormattingToString(formatMethod);
                    }
                }

                if (titleFrom[type] == null)
                {
                    titleFrom[type] = new TitleFromToString();
                }

                return(titleFrom[type].GetTitle(obj, format));
            }
        }
Exemplo n.º 2
0
        internal static string TitleOrToString(object obj, string format) {
            Type type = obj.GetType();

            if (titleFrom.ContainsKey(type) && titleFrom[type] != null) {
                return titleFrom[type].GetTitle(obj, format);
            }

            titleFrom[type] = null;

            MethodInfo titleMethod = type.GetMethod("Title", Type.EmptyTypes);
            if (titleMethod != null) {
                titleFrom[type] = new TitleFromTitleMethod(titleMethod);
            }

            if (titleFrom[type] == null) {
                IList<MethodInfo> attributedMethods = new List<MethodInfo>();
                PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (PropertyInfo property in properties) {
                    if (property.GetCustomAttribute<TitleAttribute>() != null) {
                        titleFrom[type] = new TitleFromProperty(property);
                    }
                }
            }

            if (titleFrom[type] == null) {
                MethodInfo formatMethod = type.GetMethod("ToString", new[] {typeof (string)});
                if (formatMethod != null) {
                    titleFrom[type] = new TitleFromFormattingToString(formatMethod);
                }
            }

            if (titleFrom[type] == null) {
                titleFrom[type] = new TitleFromToString();
            }

            return titleFrom[type].GetTitle(obj, format);
        }