Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3822 was 3822, checked in by swagner, 14 years ago

Implemented reviewers' comments (#863)

  • adapted item names of generic items to reflect the type of their generic parameters
File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
24using System.Reflection;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Core {
28  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
29  public sealed class ItemAttribute : Attribute {
30    private string name;
31    public string Name {
32      get { return name; }
33      set { name = value == null ? string.Empty : value; }
34    }
35    private string description;
36    public string Description {
37      get { return description; }
38      set { description = value == null ? string.Empty : value; }
39    }
40
41    public ItemAttribute() {
42      Name = string.Empty;
43      Description = string.Empty;
44    }
45    public ItemAttribute(string name, string description) {
46      Name = name;
47      Description = description;
48    }
49
50    public static string GetName(Type type) {
51      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
52      if (attribs.Length > 0) {
53        string name = ((ItemAttribute)attribs[0]).Name;
54        if (type.IsGenericType) {
55          name += "<";
56          Type[] typeParams = type.GetGenericArguments();
57          if (typeParams.Length > 0) {
58            name += GetName(typeParams[0]);
59            for (int i = 1; i < typeParams.Length; i++)
60              name += ", " + GetName(typeParams[i]);
61          }
62          name += ">";
63        }
64        return name;
65      } else {
66        return type.GetPrettyName();
67      }
68    }
69    public static string GetDescription(Type type) {
70      object[] attribs = type.GetCustomAttributes(typeof(ItemAttribute), false);
71      if (attribs.Length > 0) return ((ItemAttribute)attribs[0]).Description;
72      else return string.Empty;
73    }
74    public static Version GetVersion(Type type) {
75      Assembly assembly = Assembly.GetAssembly(type);
76      AssemblyFileVersionAttribute version = assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).
77                                             Cast<AssemblyFileVersionAttribute>().FirstOrDefault();
78      if (version != null) {
79        return new Version(version.Version);
80      } else {
81        return assembly.GetName().Version;
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.