Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Attributes/ItemAttribute.cs @ 16824

Last change on this file since 16824 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.6 KB
RevLine 
[2546]1#region License Information
2/* HeuristicLab
[15584]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2546]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;
[7201]23using System.Drawing;
[3728]24using System.Linq;
25using System.Reflection;
[2931]26using HeuristicLab.Common;
[2546]27
28namespace HeuristicLab.Core {
29  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
30  public sealed class ItemAttribute : Attribute {
[2931]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    }
[14970]41    public bool ExcludeGenericTypeInfo { get; set; }
[2546]42
[14970]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) {
[2546]46      Name = name;
47      Description = description;
[14970]48      ExcludeGenericTypeInfo = excludeGenericTypeInfo;
[2546]49    }
50
51    public static string GetName(Type type) {
[2931]52      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
[3822]53      if (attribs.Length > 0) {
[14970]54        var attribute = (ItemAttribute)attribs[0];
55        string name = attribute.Name;
56        if (!attribute.ExcludeGenericTypeInfo && type.IsGenericType) {
[3822]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      }
[2546]70    }
71    public static string GetDescription(Type type) {
[2931]72      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
[2546]73      if (attribs.Length > 0) return ((ItemAttribute)attribs[0]).Description;
[2931]74      else return string.Empty;
[2546]75    }
[3728]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    }
[7201]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    }
[2546]90  }
91}
Note: See TracBrowser for help on using the repository browser.