Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.PluginInfrastructure/BaseClasses/PluginBase.cs @ 1228

Last change on this file since 1228 was 1228, checked in by gkronber, 15 years ago

Implemented #471 (OnLoad hook for plugins).

File size: 3.8 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;
27
28namespace HeuristicLab.PluginInfrastructure {
29  /// <summary>
30  /// Default implementation of the IPlugin interface.
31  /// </summary>
32  public abstract class PluginBase : IPlugin {
33    private string name;
34    private Version version;
35    private string[] files;
36    private string description;
37
38    public PluginBase() {
39      ReadAttributes();
40    }
41
42    private void ReadAttributes() {
43      object[] pluginAttributes = this.GetType().GetCustomAttributes(typeof(ClassInfoAttribute), false);
44
45      // exactly one attribute of the type ClassInfoAttribute must be given
46      if (pluginAttributes.Length != 1) {
47        throw new InvalidPluginException();
48      }
49
50      // after the assertion we are sure that the array access will not fail
51      ClassInfoAttribute pluginAttribute = (ClassInfoAttribute)pluginAttributes[0];
52
53      // if the plugin name is not explicitly set in the attribute then the default plugin name is the FullName of the type
54      if (pluginAttribute != null && pluginAttribute.Name != null) {
55        this.name = pluginAttribute.Name;
56      } else {
57        this.name = this.GetType().FullName;
58      }
59
60      // if the version is not explicitly set in the attribute then the version of the assembly is used as default
61      if (pluginAttribute != null && pluginAttribute.Version != null) {
62        this.version = new Version(pluginAttribute.Version);
63      } else {
64        this.version = this.GetType().Assembly.GetName().Version;
65      }
66
67      // if the description is not explicitly set in the attribute then the name of name of the plugin is used as default
68      if (pluginAttribute != null && pluginAttribute.Description != null) {
69        this.description = pluginAttribute.Description;
70      } else {
71        this.description = name;
72      }
73
74      // get all attributes of type PluginFileAttribute, multiple usage is possible
75      PluginFileAttribute[] fileAttributes = (PluginFileAttribute[])this.GetType().GetCustomAttributes(typeof(PluginFileAttribute), false);
76
77      // exctract the file names from the attributes
78      this.files = new string[fileAttributes.Length];
79      int i = 0;
80      foreach (PluginFileAttribute fileAttr in fileAttributes) {
81        files[i++] = fileAttr.Filename;
82      }
83    }
84
85    #region IPlugin Members
86    public string Name {
87      get {
88        return name;
89      }
90    }
91
92    public Version Version {
93      get {
94        return version;
95      }
96    }
97
98    public string Description {
99      get {
100        return description;
101      }
102    }
103
104    public string[] Files {
105      get {
106        return files;
107      }
108    }
109
110    public virtual void OnLoad() { }
111    public virtual void OnInstall() { }
112    public virtual void OnDelete() { }
113    public virtual void OnPreUpdate() { }
114    public virtual void OnPostUpdate() { }
115
116    #endregion
117
118  }
119}
Note: See TracBrowser for help on using the repository browser.