public static NSJSValue ToObject(NSJSVirtualMachine machine, IPHostEntry host) { if (machine == null) { return(null); } if (host == null) { return(NSJSValue.Null(machine)); } NSJSObject entry = NSJSObject.New(machine); entry.Set("HostName", host.HostName); entry.Set("AddressList", ArrayAuxiliary.ToArray(machine, host.AddressList)); string[] aliases = host.Aliases; NSJSArray array = NSJSArray.New(machine, aliases.Length); for (int i = 0; i < aliases.Length; i++) { array[i] = NSJSString.New(machine, aliases[i]); } entry.Set("Aliases", array); return(entry); }
protected internal static NSJSValue ToObject(NSJSVirtualMachine machine, object obj) { if (machine == null) { return(null); } if (obj == null) { return(NSJSValue.Null(machine)); } Type owner = obj.GetType(); NSJSObject objective = NSJSObject.New(machine); foreach (MemberInfo mi in InternalCheckKeyMembers(owner).Values) { PropertyInfo pi = mi as PropertyInfo; FieldInfo fi = mi as FieldInfo; object value = null; Type clazz = null; string key = mi.Name; if (pi != null) { clazz = pi.PropertyType; value = pi.GetValue(obj, null); } else { clazz = fi.FieldType; value = fi.GetValue(obj); } NSJSValue result = null; do { if (value == null) { break; } Type element = TypeTool.GetArrayElement(clazz); if (element == null && value is IList) { result = ArrayAuxiliary.ToArray(machine, element, (IList)value); } else if (TypeTool.IsBasicType(clazz) && !TypeTool.IsIPAddress(clazz)) { result = value.As(machine); } else { result = ToObject(machine, value); } } while (false); if (result == null) { result = NSJSValue.Null(machine); } objective.Set(key, result); } NetToObjectCallables callables = InternalCheckNetToObjectCallables(owner); if (callables != null) { if (callables.funcs != null) { foreach (MethodInfo m in callables.funcs) { objective.Set(m.Name, NSJSPinnedCollection.Pinned(Complier(m))); } } if (callables.props != null) { foreach (PropertyInfo p in callables.props) { MethodInfo gm = p.GetGetMethod(); NSJSFunctionCallback get = null; NSJSFunctionCallback set = null; if (gm != null) { get = NSJSPinnedCollection.Pinned(Complier(gm)); } MethodInfo sm = p.GetSetMethod(); if (sm != null) { set = NSJSPinnedCollection.Pinned(Complier(sm)); } if (set != null || get != null) { objective.DefineProperty(p.Name, get, set); } } } objective.Set("Dispose", FDEFAULTDISPOSE); if (!objective.IsDefined("Close")) { objective.Set("Close", FDEFAULTDISPOSE); } NSJSKeyValueCollection.Set(objective, obj); } return(objective); }