Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/BaseClasses/ApplicationBase.cs @ 2402

Last change on this file since 2402 was 1189, checked in by vdorfer, 16 years ago

Created parts of the API documentation for HeuristicLab.PluginInfrastructure namespace (#331)

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections.Generic;
24using System.Text;
25using System.Diagnostics;
26
27namespace HeuristicLab.PluginInfrastructure {
28  /// <summary>
29  /// Default implementation for the IApplication interface.
30  /// </summary>
31  public abstract class ApplicationBase : IApplication {
32    private string name;
33    private Version version;
34    private string description;
35    private bool autoRestart;
36
37    /// <summary>
38    /// Initializes a new instance of <see cref="ApplicationBase"/>.
39    /// </summary>
40    public ApplicationBase() {
41      ReadAttributes();
42    }
43
44    private void ReadAttributes() {
45      object[] pluginAttributes = this.GetType().GetCustomAttributes(typeof(ClassInfoAttribute), false);
46
47      // exactly one attribute of the type ClassInfoAttribute must be given
48      if(pluginAttributes.Length != 1) {
49        throw new InvalidPluginException();
50      }
51
52      // after the assertion we are sure that the array access will not fail
53      ClassInfoAttribute pluginAttribute = (ClassInfoAttribute)pluginAttributes[0];
54      if(pluginAttribute != null) {
55        // if the plugin name is not explicitly set in the attribute then the default plugin name is the FullName of the type
56        if(pluginAttribute.Name != null) {
57          this.name = pluginAttribute.Name;
58        } else {
59          this.name = this.GetType().FullName;
60        }
61
62        // if the version is not explicitly set in the attribute then the version of the assembly is used as default
63        if(pluginAttribute.Version != null) {
64          this.version = new Version(pluginAttribute.Version);
65        } else {
66          this.version = this.GetType().Assembly.GetName().Version;
67        }
68
69        // if the description is not explicitly set in the attribute then the name of name of the application is used as default
70        if(pluginAttribute.Description != null) {
71          this.description = pluginAttribute.Description;
72        } else {
73          this.description = name;
74        }
75
76        this.autoRestart = pluginAttribute.AutoRestart;
77      }
78    }
79
80
81    #region IApplication Members
82
83    /// <summary>
84    /// Gets the name of the application.
85    /// </summary>
86    public string Name {
87      get { return name; }
88    }
89
90    /// <summary>
91    /// Gets the version of the application.
92    /// </summary>
93    public Version Version {
94      get { return version; }
95    }
96
97    /// <summary>
98    /// Gets the description of the application.
99    /// </summary>
100    public string Description {
101      get { return description; }
102    }
103
104    /// <summary>
105    /// Gets the boolean flag whether the application should automatically get restarted.
106    /// </summary>
107    public bool AutoRestart {
108      get { return autoRestart; }
109    }
110
111    /// <summary>
112    /// Runs the application.
113    /// </summary>
114    public abstract void Run();
115
116    #endregion
117  }
118}
Note: See TracBrowser for help on using the repository browser.