[3742] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17246] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3742] | 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;
|
---|
[3029] | 23 | using System.Reflection;
|
---|
| 24 | using System.Text;
|
---|
[16662] | 25 | using HEAL.Attic;
|
---|
[3029] | 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) {
|
---|
[16892] | 43 | DisentangledName = string.IsNullOrEmpty(attribute.OldName) ? attribute.Name : attribute.OldName;
|
---|
[3029] | 44 | DefaultValue = attribute.DefaultValue;
|
---|
| 45 | MemberInfo = memberInfo;
|
---|
[16892] | 46 | if (string.IsNullOrEmpty(attribute.OldName) && !attribute.AllowOneWay)
|
---|
[5324] | 47 | CheckPropertyAccess(memberInfo as PropertyInfo);
|
---|
[3029] | 48 | }
|
---|
[5324] | 49 | public StorableMemberInfo(MemberInfo memberInfo, bool allowOneWay) {
|
---|
[3029] | 50 | MemberInfo = memberInfo;
|
---|
[5324] | 51 | if (!allowOneWay)
|
---|
| 52 | CheckPropertyAccess(memberInfo as PropertyInfo);
|
---|
[3029] | 53 | }
|
---|
[5324] | 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 | }
|
---|
[3029] | 60 | public void SetDisentangledName(string name) {
|
---|
| 61 | if (DisentangledName == null)
|
---|
| 62 | DisentangledName = name;
|
---|
| 63 | }
|
---|
[5324] | 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>
|
---|
[3029] | 69 | public Type GetPropertyDeclaringBaseType() {
|
---|
[5324] | 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.");
|
---|
[3029] | 78 | }
|
---|
[4068] | 79 | }
|
---|
[3029] | 80 | } |
---|