public static string ToPrimitive(object value) { //make double value likes integer, e.g. ToPrimitive(25.0) returns "25, ToPrimitive(25.3) returns "25.3" if (value is double) { return(value.ToString()); } else if (value is Guid) { return($"new Guid(\"{value}\")"); } else if (value is CodeString) { return(value.ToString()); } else if (value is byte[]) { var hex = (value as byte[]) .Select(b => $"0x{b:X}") .Aggregate((b1, b2) => $"{b1},{b2}"); return("new byte[] {" + hex + "}"); //return "new byte[] {0x" + BitConverter.ToString((byte[])value).Replace("-", ",0x") + "}"; } return(VAL.Boxing(value).ToString()); }
public AttributeInfo(string name, params object[] args) { this.Name = name; if (args != null) { List <string> list = new List <string>(); foreach (var arg in args) { if (arg is string) { list.Add(arg as string); } else if (arg is AttributeInfoArg) { list.Add((arg as AttributeInfoArg).ToString()); } else { foreach (var propertyInfo in arg.GetType().GetProperties()) { var val = VAL.Boxing(propertyInfo.GetValue(arg)); list.Add($"{propertyInfo.Name} = {val}"); } } } this.Args = list.ToArray(); } }
public static VAL ToVAL(this DataRow dataRow, VAL val) { foreach (DataColumn dataColumn in dataRow.Table.Columns) { val[dataColumn.ColumnName] = VAL.Boxing(dataRow[dataColumn.ColumnName]); } return(val); }
public static VAL Class2VAL(object instance, VAL val) { val["ClassName"] = new VAL(instance.GetType().FullName); FieldInfo[] fields = instance.GetType().GetFields(); foreach (FieldInfo fieldInfo in fields) { if (fieldInfo.IsStatic) { continue; } Attribute[] attributes = CustomAttributeProvider.GetAttributes <NonValizedAttribute>(fieldInfo); if (attributes.Length != 0) { continue; } attributes = CustomAttributeProvider.GetAttributes <AssociationAttribute>(fieldInfo); if (attributes.Length != 0) { continue; } val[fieldInfo.Name] = VAL.Boxing(fieldInfo.GetValue(instance)); } PropertyInfo[] properties = instance.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { ValizableAttribute[] attributes = CustomAttributeProvider.GetAttributes <ValizableAttribute>(propertyInfo); if (attributes.Length != 0 && propertyInfo.CanRead) { val[propertyInfo.Name] = VAL.Boxing(propertyInfo.GetValue(instance, null)); } else { continue; } } return(val); }
public static string WriteJson(this DataTable dt, JsonStyle style, bool excludeTableName) { if (dt.Columns.Count == 1) { string json = ToJson(style, VAL.Boxing(dt.ToArray(row => row[0]))); return(json); } VAL _dt = WriteVAL(dt, style); if (excludeTableName) { return(ToJson(style, _dt)); } VAL val = new VAL(); val.AddMember(dt.TableName, _dt); return(ToJson(style, val)); }
private void PackRecord(DataRow dataRow) { PersistentObject dpo = (PersistentObject)Activator.CreateInstance(this.dpoType, new object[] { dataRow }); foreach (FieldInfo fieldInfo in this.publicFields) { object obj = fieldInfo.GetValue(dpo); if (obj != null) { VAL val = VAL.Boxing(obj); string s = val.ToString(); if (obj is float) { s = obj.ToString() + "F"; } else if (obj is string && s.Length > 100) { s = s .Replace("\\r\\n", "\r\n") .Replace("\\n", "\r\n") .Replace("\\t", "\t") .Replace("\\\"", "\"\"") .Replace("\\\\", "\\") ; pack.Statement.AppendFormat("dpo.{0} = @{1}", fieldInfo.Name, s); } else { pack.Statement.AppendFormat("dpo.{0} = {1}", fieldInfo.Name, s); } } } pack.Statement.AppendLine("list.Add(dpo)"); pack.Statement.AppendLine(); }
private static VAL Host2Valor(object host, VAL val) { if (host == null || host is System.DBNull) { val = new VAL(); } else if (host is string || host is char || host is byte || host is int || host is short || host is long || host is bool || host is double || host is float || host is decimal || host is DateTime) { val = VAL.Boxing1(host); } else if (host is IValizable) { val = ((IValizable)host).GetValData(); } else if (host is Type) { val = VAL.Script(string.Format("typeof({0})", ((Type)host).FullName)); } else if (host.GetType().IsEnum) { val = VAL.Script(HostOperation.EnumBitFlags(host)); } else if (host is ICollection) { val = VAL.Array(); foreach (object a in (ICollection)host) { val.Add(Host2Valor(a, new VAL())); } } else { VAL temp = ValizerScript.ToValor(host); if ((object)temp != null) { return(temp); } FieldInfo[] fields = host.GetType().GetFields(); foreach (FieldInfo fieldInfo in fields) { Attribute[] A = (Attribute[])fieldInfo.GetCustomAttributes(typeof(NonValizedAttribute), true); if (A.Length != 0) { continue; } object fieldValue = fieldInfo.GetValue(host); VAL persistent = ValizerScript.ToValor(fieldInfo, fieldValue); if ((object)persistent == null) { persistent = VAL.Boxing(fieldValue); if (!fieldInfo.FieldType.IsValueType && persistent.IsHostType) { persistent = Host2Valor(fieldValue, new VAL()); } } val[fieldInfo.Name] = persistent; } PropertyInfo[] properties = host.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { ValizableAttribute[] attributes = (ValizableAttribute[])propertyInfo.GetCustomAttributes(typeof(ValizableAttribute), true); if (attributes.Length == 0 || !propertyInfo.CanRead) { continue; } object propertyValue = propertyInfo.GetValue(host, null); if (propertyValue == null) { continue; } VAL persistent = ValizerScript.ToValor(propertyInfo, propertyValue); if ((object)persistent == null) { if (propertyValue is ICollection) { ICollection collection = (ICollection)propertyValue; persistent = VAL.Array(); foreach (object obj in collection) { persistent.Add(VAL.Boxing(obj)); } } else { persistent = VAL.Boxing(propertyValue); if (!propertyInfo.PropertyType.IsValueType && persistent.IsHostType) { persistent = Host2Valor(propertyValue, new VAL()); } } } val[propertyInfo.Name] = persistent; } } val.Class = host.GetType().FullName; return(val); }
public static object CallFunc(VAL funccon, object[] arguments) { if (funccon.ty == VALTYPE.funccon) { ContextInstance temp = (ContextInstance)funccon.temp; Context context = temp.context; VAL instance = temp.instance; VAL ret = CPU.ExternalUserFuncCall(funccon, instance, VAL.Boxing(arguments), context); return(ret.HostValue); } throw new HostTypeException("VAL {0} is not funccon type.", funccon); }