예제 #1
0
        public static byte[] Serialize(this IBSonNode document)
        {
            MemoryStream ms     = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(ms);

            document.GetBytes(writer);
            return(ms.ToArray());
        }
예제 #2
0
        private static T BSonToEntityG <T>(IBSonNode node) where T : class, new()
        {
            T   entity     = new T();
            var doc        = node as BSonDoc;
            var properties = typeof(T).GetProperties();

            foreach (var p in properties)
            {
                if (doc.HasProperty(p.Name) && doc[p.Name] != null)
                {
                    object val = doc[p.Name].DeserializeValue(p.PropertyType);
                    if (p.PropertyType.IsAssignableFrom(val.GetType()))
                    {
                        entity.FastSet(p.Name, val); continue;
                    }
                }
            }
            return(entity);
        }
예제 #3
0
        public static T FromBSon <T>(this IBSonNode node) //where T : class, new()
        {
            var tt = typeof(T);

            if (tt.IsValueType ||
                tt == typeof(string) ||
                tt == typeof(DateTime) ||
                tt == typeof(byte[]) ||
                tt == typeof(Guid))
            {
                if (node.Value != null && !node.IsDoc)
                {
                    if (tt.IsAssignableFrom(node.Value.GetType()))
                    {
                        return((T)node.Value);
                    }
                }
            }

            if (typeof(IEnumerable).IsAssignableFrom(tt))
            {
                if (tt.IsGenericType)
                {
                    var gmi = _BSonToIEnumG.MakeGenericMethod(tt.GetGenericArguments());
                    return((T)gmi.Invoke(null, new object[] { node }));
                }
                else
                {
                    return((T)_BSonToIEnum.Invoke(null, new object[] { node }));
                }
            }

            if (node.IsDoc)
            {
                var gmi = _BSonToEntityG.MakeGenericMethod(tt);

                return((T)gmi.Invoke(null, new object[] { node }));
            }
            return(default(T));
        }
예제 #4
0
 public static BSonDoc AsDoc(this IBSonNode node)
 {
     return(node as BSonDoc);
 }