- Timestamp:
- 03/18/09 11:48:30 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Decomposers.cs
r1348 r1354 5 5 namespace Persistence { 6 6 7 7 8 public interface IDecomposer { 8 9 bool CanSerialize(Type type); 9 IEnumerable Serialize(object o );10 object DeSerialize(IEnumerable o , Type t);10 IEnumerable Serialize(object obj); 11 object DeSerialize(IEnumerable objects, Type type); 11 12 } 12 13 14 13 15 public class EnumerableDecomposer : IDecomposer { 16 14 17 public bool CanSerialize(Type type) { 15 if (type.GetInterface("IEnumerable") == null) 16 return false; 17 if (type.GetMethod("GetEnumerator", new Type[] { }) == null) 18 return false; 18 return 19 type.GetInterface("IEnumerable") != null && 20 type.GetMethod("Add") != null && 21 type.GetMethod("Add").GetParameters().Length == 1 && 22 type.GetConstructor( 23 BindingFlags.Public | 24 BindingFlags.NonPublic | 25 BindingFlags.Instance, 26 null, Type.EmptyTypes, null) != null; 27 } 28 29 public IEnumerable Serialize(object obj) { 30 return (IEnumerable)obj; 31 } 32 33 public object DeSerialize(IEnumerable objects, Type type) { 34 object instance = Activator.CreateInstance(type, true); 19 35 MethodInfo addMethod = type.GetMethod("Add"); 20 if (addMethod == null)21 return false;22 if (addMethod.GetParameters().Length != 1)23 return false;24 return true;25 }26 public IEnumerable Serialize(object o) {27 return (IEnumerable)o;28 }29 public object DeSerialize(IEnumerable objects, Type t) {30 object instance = Activator.CreateInstance(t, true);31 36 foreach (object o in objects) { 32 t.GetMethod("Add").Invoke(instance, new[] { o });37 addMethod.Invoke(instance, new[] {o}); 33 38 } 34 39 return instance; 35 40 } 41 36 42 } 37 43 44 38 45 public class ArrayDecomposer : IDecomposer { 46 39 47 public bool CanSerialize(Type type) { 40 48 return type.IsArray || type == typeof(Array); 41 49 } 50 42 51 public IEnumerable Serialize(object array) { 43 52 Array a = (Array)array; … … 50 59 } 51 60 } 61 52 62 public object DeSerialize(IEnumerable elements, Type t) { 53 63 IEnumerator e = elements.GetEnumerator(); … … 77 87 } 78 88 89 79 90 public class KeyValuePairDecomposer : IDecomposer { 80 public bool CanSerialize(Type type) { 91 92 public bool CanSerialize(Type type) { 81 93 return type.IsGenericType && 82 type.GetGenericTypeDefinition().Name == "KeyValuePair`2"; 94 type.GetGenericTypeDefinition() == 95 typeof (KeyValuePair<int, int>).GetGenericTypeDefinition(); 83 96 } 97 84 98 public IEnumerable Serialize(object o) { 85 99 Type t = o.GetType(); … … 87 101 yield return t.GetProperty("Value").GetValue(o, null); 88 102 } 103 89 104 public object DeSerialize(IEnumerable o, Type t) { 90 105 return Activator.CreateInstance(t, … … 92 107 } 93 108 } 109 94 110 95 111 public class DictionaryDecomposer : IDecomposer { -
branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj
r1349 r1354 21 21 <Optimize>false</Optimize> 22 22 <OutputPath>bin\Debug\</OutputPath> 23 <DefineConstants> DEBUG;TRACE</DefineConstants>23 <DefineConstants>TRACE;DEBUG</DefineConstants> 24 24 <ErrorReport>prompt</ErrorReport> 25 25 <WarningLevel>4</WarningLevel> -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1348 r1354 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Collections;4 3 using System.IO; 5 using System.Reflection;6 4 7 5 namespace Persistence.Test { … … 120 118 } 121 119 120 122 121 public static void Main() { 123 Test1();122 //Test1(); 124 123 //Test2(); 125 124 //SpeedTest(); -
branches/New Persistence Exploration/Persistence/Test/Test.csproj
r1348 r1354 20 20 <Optimize>false</Optimize> 21 21 <OutputPath>bin\Debug\</OutputPath> 22 <DefineConstants> DEBUG;TRACE</DefineConstants>22 <DefineConstants>TRACE;DEBUG</DefineConstants> 23 23 <ErrorReport>prompt</ErrorReport> 24 24 <WarningLevel>4</WarningLevel>
Note: See TracChangeset
for help on using the changeset viewer.