Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/Descriptors/HookDescriptor.cs @ 6221

Last change on this file since 6221 was 6221, checked in by epitzer, 13 years ago

streamline access and don't eat exceptions when access unavailable types and members (#1530)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
21using System;
22using System.Reflection;
23using System.Text;
24using HeuristicLab.Persistence.Auxiliary;
25
26namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable.Descriptors {
27
28  /// <summary>
29  /// Describes a storable hook method of a storable type.
30  /// </summary>
31  [StorableClass]
32  public sealed class HookDescriptor {
33
34    #region Fields & Properties
35    /// <summary>
36    /// The type that declared this hook.
37    /// </summary>
38    [Storable]
39    public TypeDescriptor DeclaringType { get; set; }
40
41    /// <summary>
42    /// The name of the method that represents this hook.
43    /// </summary>
44    [Storable]
45    public string MethodName { get; set; }
46
47    /// <summary>
48    /// The type of the hook.
49    /// </summary>
50    [Storable]
51    public HookType HookType { get; set; }
52
53    internal MethodInfo handle;
54    /// <summary>
55    /// The MethodInfo of the hook as obtained by reflection.
56    /// </summary>
57    public MethodInfo Handle {
58      get {
59        if (handle == null)
60          handle = DeclaringType.Handle.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
61        return handle;
62      }
63    }
64    #endregion
65
66    [StorableConstructor]
67    private HookDescriptor(bool deserializing) { }
68
69    /// <summary>
70    /// Creates a new hook description.
71    /// </summary>
72    /// <param name="declaringType">The declaring type.</param>
73    /// <param name="methodInfo">The MethodInfo as obtained by reflection.</param>
74    /// <param name="hookType">The hook type.</param>
75    public HookDescriptor(TypeDescriptor declaringType, MethodInfo methodInfo, HookType hookType) {
76      DeclaringType = declaringType;
77      MethodName = methodInfo.Name;
78      HookType = hookType;
79      handle = methodInfo;
80    }
81
82    /// <summary>
83    /// Invokes the hook method on the given object.
84    /// </summary>
85    public void Invoke(object o) {
86      Handle.Invoke(o, Type.EmptyTypes);
87    }
88
89    /// <summary>
90    /// Creates a string representation of this hook description.
91    /// </summary>
92    public override string ToString() {
93      return new StringBuilder()
94        .Append(HookType.ToString())
95        .Append(' ')
96        .Append(TypeNameParser.Parse(DeclaringType.AssemblyQualifiedName).ClassName)
97        .Append('.')
98        .Append(MethodName)
99        .Append("()")
100        .ToString();
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.