#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Persistence.Auxiliary;
namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable.Descriptors {
///
/// Describes a storable hook method of a storable type.
///
[StorableClass]
public sealed class HookDescriptor {
#region Fields & Properties
///
/// The type that declared this hook.
///
[Storable]
public TypeDescriptor DeclaringType { get; set; }
///
/// The name of the method that represents this hook.
///
[Storable]
public string MethodName { get; set; }
///
/// The type of the hook.
///
[Storable]
public HookType HookType { get; set; }
private MethodInfo handle;
///
/// The MethodInfo of the hook as obtained by reflection.
///
public MethodInfo Handle {
get {
try {
if (handle == null)
handle = DeclaringType.Handle.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
} catch { }
return handle;
}
internal set {
handle = value;
}
}
#endregion
[StorableConstructor]
private HookDescriptor(bool deserializing) { }
///
/// Creates a new hook description.
///
/// The declaring type.
/// The MethodInfo as obtained by reflection.
/// The hook type.
public HookDescriptor(TypeDescriptor declaringType, MethodInfo methodInfo, HookType hookType) {
DeclaringType = declaringType;
MethodName = methodInfo.Name;
HookType = hookType;
handle = methodInfo;
}
///
/// Invokes the hook method on the given object.
///
public void Invoke(object o) {
Handle.Invoke(o, Type.EmptyTypes);
}
///
/// Creates a string representation of this hook description.
///
public override string ToString() {
return new StringBuilder()
.Append(HookType.ToString())
.Append(' ')
.Append(TypeNameParser.Parse(DeclaringType.AssemblyQualifiedName).ClassName)
.Append('.')
.Append(MethodName)
.Append("()")
.ToString();
}
}
}