Free cookie consent management tool by TermsFeed Policy Generator

source: branches/gteufl/HeuristicLab.Core/3.3/Attributes/ItemAttribute.cs @ 11571

Last change on this file since 11571 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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
42    public ItemAttribute() {
43      Name = string.Empty;
44      Description = string.Empty;
45    }
46    public ItemAttribute(string name, string description) {
47      Name = name;
48      Description = description;
49    }
50
51    public static string GetName(Type type) {
52      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
53      if (attribs.Length > 0) {
54        string name = ((ItemAttribute)attribs[0]).Name;
55        if (type.IsGenericType) {
56          name += "<";
57          Type[] typeParams = type.GetGenericArguments();
58          if (typeParams.Length > 0) {
59            name += GetName(typeParams[0]);
60            for (int i = 1; i < typeParams.Length; i++)
61              name += ", " + GetName(typeParams[i]);
62          }
63          name += ">";
64        }
65        return name;
66      } else {
67        return type.GetPrettyName();
68      }
69    }
70    public static string GetDescription(Type type) {
71      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
72      if (attribs.Length > 0) return ((ItemAttribute)attribs[0]).Description;
73      else return string.Empty;
74    }
75    public static Version GetVersion(Type type) {
76      Assembly assembly = Assembly.GetAssembly(type);
77      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
78                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
79      if (version != null) {
80        return new Version(version.Version);
81      } else {
82        return assembly.GetName().Version;
83      }
84    }
85    public static Image GetImage(Type type) {
86      var staticItemImageProperty = type.GetProperty("StaticItemImage", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
87      return staticItemImageProperty == null ? null : (Image)staticItemImageProperty.GetValue(null, null);
88    }
89  }
90}
Note: See TracBrowser for help on using the repository browser.