Example #1
0
        public static string Encode(object host, bool persistent)
        {
            Type type;

            if (host is MethodInfo)
            {
                MethodInfo methodInfo = (MethodInfo)host;
                if (methodInfo.IsStatic)
                {
                    return(methodInfo.ReflectedType.FullName + "." + methodInfo.Name);
                }
                else
                {
                    return(methodInfo.Name);
                }
            }
            else if (host is Type)
            {
                type = (Type)host;
                return(string.Format("typeof({0})", type.FullName));
            }
            else
            {
                type = host.GetType();
            }

            if (type.IsEnum)            //处理enum常量
            {
                return(HostOperation.EnumBitFlags(host));
            }

            if (host is DateTime)
            {
                return(string.Format("new {0}({1})", typeof(DateTime).FullName, ((DateTime)host).Ticks));
            }


            VAL val = HostValization.Host2Valor(host);

            if (persistent)
            {
                return(val.Valor);
            }
            else
            {
                //default contructor
                if (HostCoding.HasContructor(type, new Type[] {}))
                {
                    return(string.Format("new {0}()", type.FullName));   //有缺省的constructor
                }
                if (type.FullName == host.ToString())
                {
                    return(string.Format("new {0}(...)", type.FullName));
                }
                else
                {
                    return(string.Format("new {0}({1})", type.FullName, host));
                }
            }
        }
Example #2
0
        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);
        }