public MPObject Encode(IConverterContext ctx, Type type, object obj) { if (obj == null) { return(MPObject.Nil); } var map = new Dictionary <MPObject, MPObject>(); var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Select(p => (prop: p, attr: p.GetCustomAttribute <TFAttributeAttribute>())) .Where(pa => pa.attr != null) .ToArray(); foreach (var p in props) { var name = p.attr.Name; var value = p.prop.GetValue(obj); var propName = ctx.Encode(typeof(string), name); var propType = p.prop.PropertyType; var propValue = (value == null && p.attr.Computed) ? new MPObject(MPType.Ext, UnknownExtension) : ctx.Encode(propType, value); map.Add(propName, propValue); } return(new MPObject(MPType.Map, map)); }
public MPObject Encode(IConverterContext ctx, Type type, object obj) { if (obj == null) { return(MPObject.Nil); } var map = new Dictionary <MPObject, MPObject>(); var props = Resolver.ResolvePropertyNames(type); foreach (var prop in props) { var propInfo = prop.Value; if (propInfo.GetIndexParameters()?.Length > 0) { // Skip indexer properties continue; } var propName = ctx.Encode(typeof(string), prop.Key); var propType = propInfo.PropertyType; var propValue = ctx.Encode(propType, propInfo.GetValue(obj)); map.Add(propName, propValue); } return(new MPObject(MPType.Map, map)); }
public MPObject Encode(IConverterContext ctx, Type type, object obj) { if (obj == null) { return(MPObject.Nil); } var itemType = typeof(object); var gt = Util.GetSubclassOfGenericTypeDefinition(typeof(IList <>), type); if (type.IsArray) { itemType = type.GetElementType(); } else if (gt != null) { itemType = gt.GenericTypeArguments[0]; } var arr = new List <MPObject>(); var enm = (IEnumerable)obj; foreach (var item in enm) { arr.Add(ctx.Encode(itemType, item)); } return(new MPObject(MPType.Array, arr)); }
public MPObject Encode(IConverterContext ctx, Type type, object obj) { if (obj == null) { return(MPObject.Nil); } var keyType = typeof(object); var valType = typeof(object); IEnumerable keys; Func <object, object> getter; var gt = Util.GetSubclassOfGenericTypeDefinition(typeof(IDictionary <,>), type); if (gt != null) { keyType = gt.GenericTypeArguments[0]; valType = gt.GenericTypeArguments[1]; keys = (IEnumerable)gt.GetProperty("Keys").GetValue(obj); var itemProp = gt.GetProperty("Item"); var index = new object[] { null }; getter = k => { index[0] = k; return(itemProp.GetValue(obj, index)); }; } else { var d = (IDictionary)obj; keys = d.Keys; getter = k => d[k]; } var map = new Dictionary <MPObject, MPObject>(); foreach (var k in keys) { var v = getter(k); var mpoKey = ctx.Encode(keyType, k); var mpoVal = ctx.Encode(valType, v); map.Add(mpoKey, mpoVal); } return(new MPObject(MPType.Map, map)); }
public MPObject Encode(IConverterContext ctx, Type type, object obj) { Type narrowType = null; if (obj == null) { return(MPObject.Nil); } if (obj is bool || obj is bool? || obj is byte || obj is byte? || obj is sbyte || obj is sbyte? || obj is short || obj is short? || obj is ushort || obj is ushort? || obj is int || obj is int? || obj is uint || obj is uint? || obj is long || obj is long? || obj is ulong || obj is ulong? || obj is char || obj is char? || obj is float || obj is float? || obj is double || obj is double? || obj is decimal || obj is decimal?|| obj is byte[] || obj is string ) { narrowType = obj.GetType(); } else if (obj is IDictionary || Util.GetSubclassOfGenericTypeDefinition( typeof(IDictionary <,>), obj.GetType()) != null) { narrowType = typeof(Hashtable); } else if (obj is Array || obj is IList || (Util.GetSubclassOfGenericTypeDefinition( typeof(IList <>), obj.GetType()) != null)) { narrowType = typeof(ArrayList); } if (narrowType != null) { return(ctx.Encode(narrowType, obj)); } throw new MPConversionException(type); }