1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Reflection;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Persistence.Core;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
|
---|
28 |
|
---|
29 | internal sealed class StorableMemberInfo {
|
---|
30 | public MemberInfo MemberInfo { get; private set; }
|
---|
31 | public string DisentangledName { get; private set; }
|
---|
32 | public object DefaultValue { get; private set; }
|
---|
33 | public string FullyQualifiedMemberName {
|
---|
34 | get {
|
---|
35 | return new StringBuilder()
|
---|
36 | .Append(MemberInfo.ReflectedType.FullName)
|
---|
37 | .Append('.')
|
---|
38 | .Append(MemberInfo.Name)
|
---|
39 | .ToString();
|
---|
40 | }
|
---|
41 | }
|
---|
42 | public StorableMemberInfo(StorableAttribute attribute, MemberInfo memberInfo) {
|
---|
43 | DisentangledName = attribute.Name;
|
---|
44 | DefaultValue = attribute.DefaultValue;
|
---|
45 | MemberInfo = memberInfo;
|
---|
46 | if (!attribute.AllowOneWay)
|
---|
47 | CheckPropertyAccess(memberInfo as PropertyInfo);
|
---|
48 | }
|
---|
49 | public StorableMemberInfo(MemberInfo memberInfo, bool allowOneWay) {
|
---|
50 | MemberInfo = memberInfo;
|
---|
51 | if (!allowOneWay)
|
---|
52 | CheckPropertyAccess(memberInfo as PropertyInfo);
|
---|
53 | }
|
---|
54 | private static void CheckPropertyAccess(PropertyInfo propertyInfo) {
|
---|
55 | if (propertyInfo == null)
|
---|
56 | return;
|
---|
57 | if (!propertyInfo.CanRead || !propertyInfo.CanWrite)
|
---|
58 | throw new PersistenceException("Properties must be readable and writable or explicity enable one way serialization.");
|
---|
59 | }
|
---|
60 | public void SetDisentangledName(string name) {
|
---|
61 | if (DisentangledName == null)
|
---|
62 | DisentangledName = name;
|
---|
63 | }
|
---|
64 | /// <summary>
|
---|
65 | /// Gets the type who first defined this property in the class hierarchy when the
|
---|
66 | /// property has subsequently been overridden but not shadowed with <code>new</code>.
|
---|
67 | /// </summary>
|
---|
68 | /// <returns>The properties base type.</returns>
|
---|
69 | public Type GetPropertyDeclaringBaseType() {
|
---|
70 | PropertyInfo pi = MemberInfo as PropertyInfo;
|
---|
71 | if (pi == null)
|
---|
72 | throw new PersistenceException("fields don't have a declaring base type, directly use FullyQualifiedMemberName instead");
|
---|
73 | if (pi.CanRead)
|
---|
74 | return pi.GetGetMethod(true).GetBaseDefinition().DeclaringType;
|
---|
75 | if (pi.CanWrite)
|
---|
76 | return pi.GetSetMethod(true).GetBaseDefinition().DeclaringType;
|
---|
77 | throw new InvalidOperationException("property has neigher a getter nor a setter.");
|
---|
78 | }
|
---|
79 | }
|
---|
80 | } |
---|