Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/BaseClasses/PluginBase.cs @ 2475

Last change on this file since 2475 was 2475, checked in by gkronber, 14 years ago

worked on plugin infrastructure refactoring. #799

File size: 3.3 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.Reflection;
26using System.Diagnostics;
27using System.Linq;
28
29namespace HeuristicLab.PluginInfrastructure {
30  /// <summary>
31  /// Default implementation of the IPlugin interface.
32  /// </summary>
33  public abstract class PluginBase : IPlugin {
34    /// <summary>
35    /// Initializes a new instance of <see cref="PluginBase"/>.
36    /// </summary>
37    public PluginBase() { }
38
39    private PluginDescriptionAttribute PluginDescriptionAttribute {
40      get {
41        object[] pluginAttributes = this.GetType().GetCustomAttributes(typeof(PluginDescriptionAttribute), false);
42        // exactly one attribute of the type PluginDescriptionAttribute must be given
43        if (pluginAttributes.Length != 1) {
44          throw new InvalidPluginException("Found multiple PluginDescriptionAttributes on type " + this.GetType());
45        }
46        return (PluginDescriptionAttribute)pluginAttributes[0];
47      }
48    }
49
50    #region IPlugin Members
51    /// <inheritdoc />
52    public string Name {
53      get {
54        return PluginDescriptionAttribute.Name;
55      }
56    }
57
58    /// <inheritdoc />
59    public Version Version {
60      get {
61        var pluginAttribute = PluginDescriptionAttribute;
62        // if the version is not set in the attribute then the version of the assembly is used as default
63        if (string.IsNullOrEmpty(pluginAttribute.Version)) {
64          return this.GetType().Assembly.GetName().Version;
65        } else {
66          return new Version(pluginAttribute.Version);
67        }
68      }
69    }
70
71    /// <inheritdoc />
72    public string Description {
73      get {
74        var pluginAttribute = PluginDescriptionAttribute;
75        // if the description is not explicitly set in the attribute then the name of the plugin is used as default
76        if (string.IsNullOrEmpty(pluginAttribute.Description)) {
77          return pluginAttribute.Name;
78        } else {
79          return pluginAttribute.Description;
80        }
81      }
82    }
83
84    /// <inheritdoc/>
85    public IEnumerable<string> FileNames {
86      get {
87        // get all attributes of type PluginFileAttribute, multiple usage is possible
88        return from x in this.GetType().GetCustomAttributes(typeof(PluginFileAttribute), false)
89               let pluginFileAttr = (PluginFileAttribute)x
90               select pluginFileAttr.Filename;
91      }
92    }
93
94    /// <inhertitdoc>
95    public virtual void OnLoad() { }
96    #endregion
97
98  }
99}
Note: See TracBrowser for help on using the repository browser.