Changeset 1338
- Timestamp:
- 03/13/09 10:34:57 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 5 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence.sln
r1332 r1338 3 3 # Visual Studio 2008 4 4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Persistence", "Persistence\Persistence.csproj", "{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}" 5 EndProject 6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{E2245078-6F40-449E-A370-01A97A7C5E6A}" 5 7 EndProject 6 8 Global … … 18 20 {102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}.Release|Any CPU.Build.0 = Release|Any CPU 19 21 {102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}.Release|x86.ActiveCfg = Release|Any CPU 22 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Debug|x86.ActiveCfg = Debug|Any CPU 25 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Release|Any CPU.Build.0 = Release|Any CPU 27 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Release|x86.ActiveCfg = Release|Any CPU 20 28 EndGlobalSection 21 29 GlobalSection(SolutionProperties) = preSolution -
branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj
r1329 r1338 7 7 <SchemaVersion>2.0</SchemaVersion> 8 8 <ProjectGuid>{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}</ProjectGuid> 9 <OutputType> Exe</OutputType>9 <OutputType>Library</OutputType> 10 10 <AppDesignerFolder>Properties</AppDesignerFolder> 11 11 <RootNamespace>Persistence</RootNamespace> … … 13 13 <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 14 14 <FileAlignment>512</FileAlignment> 15 <StartupObject>Persistence.Test.NewSerializationTest</StartupObject> 15 <StartupObject> 16 </StartupObject> 16 17 </PropertyGroup> 17 18 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> … … 53 54 <ItemGroup> 54 55 <Compile Include="DeSerializer.cs" /> 56 <Compile Include="Properties\AssemblyInfo.cs" /> 55 57 <Compile Include="Serializer.cs" /> 56 58 <Compile Include="CompoundSerializers.cs" /> 57 59 <Compile Include="PrimitiveSerializers.cs" /> 60 <Compile Include="Test\NewSerializationTest.cs" /> 58 61 <Compile Include="Tokens.cs" /> 59 62 <Compile Include="Util.cs" /> 60 63 <Compile Include="Test\StorableAttributeTests.cs" /> 61 64 <Compile Include="StorableAttribute.cs" /> 62 <Compile Include="Test\NewSerializationTest.cs" />63 65 <Compile Include="Test\SerializationTest.cs" /> 64 66 <Compile Include="XmlFormatter.cs" /> 65 67 <Compile Include="XmlParser.cs" /> 66 68 </ItemGroup> 69 <PropertyGroup> 70 <PreBuildEvent>SubWCRev "$(ProjectDir)\" "$(ProjectDir)\Properties\AssemblyInfo.frame" "$(ProjectDir)\Properties\AssemblyInfo.cs"</PreBuildEvent> 71 </PropertyGroup> 67 72 <ItemGroup> 68 73 <Folder Include="Interfaces\" /> -
branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs
r1332 r1338 51 51 } 52 52 53 class CloningFactory {53 public class CloningFactory { 54 54 55 55 private static readonly CloningFactory instance = new CloningFactory(); … … 208 208 } 209 209 } 210 } 211 212 public interface IDataMemberAccessor { 213 string Name { get; } 214 Type Type { get; } 215 object DefaultValue { get; } 216 object Get(); 217 void Set(object value); 218 } 210 } 219 211 220 public class DataMemberAccessor : IDataMemberAccessor{212 public class DataMemberAccessor { 221 213 222 214 public delegate object Getter(); 223 215 public delegate void Setter(object value); 224 216 225 private readonly Getter getter; 226 private readonly Setter setter; 217 public readonly Getter Get; 218 public readonly Setter Set; 219 public readonly string Name; 220 public readonly Type Type; 221 public readonly object DefaultValue; 227 222 228 223 public DataMemberAccessor( … … 232 227 if (memberInfo.MemberType == MemberTypes.Field) { 233 228 FieldInfo fieldInfo = (FieldInfo)memberInfo; 234 getter= () => fieldInfo.GetValue(obj);235 setter= value => fieldInfo.SetValue(obj, value);229 Get = () => fieldInfo.GetValue(obj); 230 Set = value => fieldInfo.SetValue(obj, value); 236 231 Type = fieldInfo.FieldType; 237 232 } else if (memberInfo.MemberType == MemberTypes.Property) { … … 241 236 "Storable properties must implement both a Get and a Set Accessor. "); 242 237 } 243 getter= () => propertyInfo.GetValue(obj, null);244 setter= value => propertyInfo.SetValue(obj, value, null);238 Get = () => propertyInfo.GetValue(obj, null); 239 Set = value => propertyInfo.SetValue(obj, value, null); 245 240 Type = propertyInfo.PropertyType; 246 241 } else { … … 258 253 Type = type; 259 254 DefaultValue = defaultValue; 260 this.getter= getter;261 this.setter= setter;255 Get = getter; 256 Set = setter; 262 257 } 263 258 264 259 public DataMemberAccessor(object o) { 260 Name = null; 265 261 Type = o.GetType(); 266 262 DefaultValue = null; 267 getter = () => o; 268 setter = null; 269 } 270 271 public string Name { get; private set; } 272 public Type Type { get; private set; } 273 public object DefaultValue { get; private set; } 274 public object Get() { 275 return getter(); 276 } 277 public void Set(object value) { 278 setter(value); 279 } 263 Get = () => o; 264 Set = null; 265 } 280 266 public override string ToString() { 281 267 return String.Format("DataMember({0}, {1}, {2}, {3}, {4}", … … 283 269 Type == null ? "<null>" : Type.FullName, 284 270 DefaultValue ?? "<null>", 285 getter.Method,286 setter.Method);271 Get.Method, 272 Set.Method); 287 273 } 288 274 } -
branches/New Persistence Exploration/Persistence/Persistence/Test/NewSerializationTest.cs
r1336 r1338 141 141 } 142 142 } 143 144 public class NewSerializationTest {145 146 public static void Test1() {147 Root r = new Root();148 r.selfReferences = new List<Root> {r, r};149 r.c = new Custom {r = r};150 r.dict.Add("one", 1);151 r.dict.Add("two", 2);152 r.dict.Add("three", 3);153 Serializer s = new Serializer(r);154 Persistence.XmlFormatter xmlFormatter = new XmlFormatter();155 //StreamWriter writer = new StreamWriter("test.xml");156 foreach (ISerializationToken token in s) {157 string line = xmlFormatter.Format(token);158 //writer.Write(line);159 Console.Out.Write(line);160 }161 //writer.Close();162 XmlParser parser = new XmlParser(new StreamReader("test.xml"));163 DeSerializer deSerializer = new DeSerializer();164 object o = deSerializer.DeSerialize(parser);165 Root t = CloningFactory.DefaultClone(r);166 Console.Out.WriteLine(Util.AutoFormat(o, true));167 }168 169 public static void Test2() {170 Manager m = new Manager();171 Serializer s = new Serializer(m);172 Persistence.XmlFormatter xmlFormatter = new XmlFormatter();173 StreamWriter writer = new StreamWriter("test2.xml");174 foreach (ISerializationToken token in s) {175 string line = xmlFormatter.Format(token);176 writer.Write(line);177 Console.Out.Write(line);178 }179 writer.Close();180 XmlParser parser = new XmlParser(new StreamReader("test2.xml"));181 DeSerializer deSerializer = new DeSerializer();182 object o = deSerializer.DeSerialize(parser);183 Manager n = CloningFactory.DefaultClone(m);184 Console.Out.WriteLine(Util.AutoFormat(o, true));185 }186 187 public static void SpeedTest() {188 StorableObject storable = new StorableObject();189 CloneableObject cloneable = new CloneableObject();190 Console.Write("initializing...");191 storable.Init();192 cloneable.Init();193 Console.WriteLine("done");194 List<StorableObject> storableClones = new List<StorableObject>();195 List<CloneableObject> clonableClones = new List<CloneableObject>();196 CloningFactory cloningFactory = new CloningFactory();197 for (int i = 0; i < 100000; i++) {198 Console.Write("cloning storable.. ");199 storableClones.Add(cloningFactory.Clone(storable));200 Console.WriteLine();201 Console.Write("cloning cloneable.. ");202 clonableClones.Add((CloneableObject)cloneable.Clone());203 Console.WriteLine();204 }205 }206 207 public static void SpeedTest2() {208 Root r = new Root();209 r.selfReferences = new List<Root> {r, r};210 r.c = new Custom {r = r};211 r.dict.Add("one", 1);212 r.dict.Add("two", 2);213 r.dict.Add("three", 3);214 215 CloneableRoot cr = new CloneableRoot();216 cr.selfReferences = new List<CloneableRoot> {cr, cr};217 cr.c = new CloneableCustom {r = cr};218 cr.dict.Add("one", 1);219 cr.dict.Add("two", 2);220 cr.dict.Add("three", 3);221 List<Root> storableClones = new List<Root>();222 List<CloneableRoot> clonableClones = new List<CloneableRoot>();223 CloningFactory cloningFactory = new CloningFactory();224 225 DateTime start = DateTime.Now;226 Console.Write("cloning storable.. ");227 for (int i = 0; i < 100000; i++) {228 storableClones.Add(cloningFactory.Clone(r));229 }230 Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));231 232 start = DateTime.Now;233 Console.Write("cloning storable again.. ");234 for (int i = 0; i < 100000; i++) {235 storableClones.Add(cloningFactory.Clone(r));236 }237 Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));238 239 start = DateTime.Now;240 Console.Write("cloning cloneable.. ");241 for (int i = 0; i < 100000; i++) {242 clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary<object, object>()));243 }244 Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks));245 clonableClones.Clear();246 247 }248 249 public class Base {250 [Storable] private int baseInt = 3;251 }252 253 public class Derived: Base {254 [Storable] private int derivedInt = 5;255 }256 257 public static void Main() {258 int[] i = {1, 2, 3};259 object o = i;260 Type type = o.GetType().BaseType.BaseType;261 foreach (MemberInfo mi in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic )) {262 if ( mi.MemberType == MemberTypes.Field || mi.MemberType == MemberTypes.Property )263 Console.WriteLine(mi.Name);264 }265 Test1();266 //Test2();267 //SpeedTest();268 //SpeedTest2();269 Console.In.ReadLine();270 }271 }272 143 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1330 r1338 51 51 } 52 52 53 private string FormatBegin(ISerializationToken token) { 53 private string FormatBegin(ISerializationToken token) { 54 54 BeginToken beginToken = (BeginToken)token; 55 55 string result = Prefix + … … 58 58 { 59 59 {"name", beginToken.Accessor.Name}, 60 {"type", beginToken.Accessor.Get().GetType(). ToString()},60 {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName}, 61 61 {"id", beginToken.Id.ToString()} 62 62 }, NodeType.Start) + "\n"; … … 74 74 Dictionary<string, string> attributes = 75 75 new Dictionary<string, string> { 76 {"type", dataToken.Accessor.Get().GetType(). ToString()}};77 if ( dataToken.Accessor.Name != "")76 {"type", dataToken.Accessor.Get().GetType().AssemblyQualifiedName}}; 77 if ( !string.IsNullOrEmpty(dataToken.Accessor.Name) ) 78 78 attributes.Add("name", dataToken.Accessor.Name); 79 79 if ( dataToken.Id != null ) -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1330 r1338 57 57 } 58 58 private IEnumerator<IParseToken> ParseComposite() { 59 string name = reader.GetAttribute("name"); 59 string name = reader.GetAttribute("name"); 60 60 Type type = Type.GetType(reader.GetAttribute("type")); 61 61 int id = int.Parse(reader.GetAttribute("id"));
Note: See TracChangeset
for help on using the changeset viewer.