Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Attributes/ItemAttribute.cs @ 15604

Last change on this file since 15604 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using System.Linq;
25using System.Reflection;
26using HeuristicLab.Common;
27
28namespace HeuristicLab.Core {
29  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
30  public sealed class ItemAttribute : Attribute {
31    private string name;
32    public string Name {
33      get { return name; }
34      set { name = value == null ? string.Empty : value; }
35    }
36    private string description;
37    public string Description {
38      get { return description; }
39      set { description = value == null ? string.Empty : value; }
40    }
41    public bool ExcludeGenericTypeInfo { get; set; }
42
43    public ItemAttribute() : this(string.Empty, string.Empty, false) { }
44    public ItemAttribute(string name, string description) : this(name, description, false) { }
45    public ItemAttribute(string name, string description, bool excludeGenericTypeInfo) {
46      Name = name;
47      Description = description;
48      ExcludeGenericTypeInfo = excludeGenericTypeInfo;
49    }
50
51    public static string GetName(Type type) {
52      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
53      if (attribs.Length > 0) {
54        var attribute = (ItemAttribute)attribs[0];
55        string name = attribute.Name;
56        if (!attribute.ExcludeGenericTypeInfo && type.IsGenericType) {
57          name += "<";
58          Type[] typeParams = type.GetGenericArguments();
59          if (typeParams.Length > 0) {
60            name += GetName(typeParams[0]);
61            for (int i = 1; i < typeParams.Length; i++)
62              name += ", " + GetName(typeParams[i]);
63          }
64          name += ">";
65        }
66        return name;
67      } else {
68        return type.GetPrettyName();
69      }
70    }
71    public static string GetDescription(Type type) {
72      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
73      if (attribs.Length > 0) return ((ItemAttribute)attribs[0]).Description;
74      else return string.Empty;
75    }
76    public static Version GetVersion(Type type) {
77      Assembly assembly = Assembly.GetAssembly(type);
78      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
79                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
80      if (version != null) {
81        return new Version(version.Version);
82      } else {
83        return assembly.GetName().Version;
84      }
85    }
86    public static Image GetImage(Type type) {
87      var staticItemImageProperty = type.GetProperty("StaticItemImage", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
88      return staticItemImageProperty == null ? null : (Image)staticItemImageProperty.GetValue(null, null);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.