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