Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/09 11:48:30 (15 years ago)
Author:
epitzer
Message:

minor code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Decomposers.cs

    r1348 r1354  
    55namespace Persistence {
    66 
     7
    78  public interface IDecomposer {
    89    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);
    1112  }
    1213
     14
    1315  public class EnumerableDecomposer : IDecomposer {
     16
    1417    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);
    1935      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);
    3136      foreach (object o in objects) {
    32         t.GetMethod("Add").Invoke(instance, new[] { o });
     37        addMethod.Invoke(instance, new[] {o});       
    3338      }
    3439      return instance;
    3540    }
     41
    3642  }
    3743
     44
    3845  public class ArrayDecomposer : IDecomposer {
     46
    3947    public bool CanSerialize(Type type) {
    4048      return type.IsArray || type == typeof(Array);
    4149    }
     50
    4251    public IEnumerable Serialize(object array) {
    4352      Array a = (Array)array;
     
    5059      }
    5160    }
     61
    5262    public object DeSerialize(IEnumerable elements, Type t) {
    5363      IEnumerator e = elements.GetEnumerator();
     
    7787  }
    7888
     89
    7990  public class KeyValuePairDecomposer : IDecomposer {   
    80     public bool CanSerialize(Type type) {     
     91
     92    public bool CanSerialize(Type type) {
    8193      return type.IsGenericType &&
    82         type.GetGenericTypeDefinition().Name == "KeyValuePair`2";
     94             type.GetGenericTypeDefinition() ==
     95             typeof (KeyValuePair<int, int>).GetGenericTypeDefinition();
    8396    }
     97
    8498    public IEnumerable Serialize(object o) {     
    8599      Type t = o.GetType();
     
    87101      yield return t.GetProperty("Value").GetValue(o, null);
    88102    }
     103
    89104    public object DeSerialize(IEnumerable o, Type t) {     
    90105      return Activator.CreateInstance(t,
     
    92107    }   
    93108  }
     109
    94110
    95111  public class DictionaryDecomposer : IDecomposer {
Note: See TracChangeset for help on using the changeset viewer.