Ejemplo n.º 1
0
        public static void FillSerializationInfo(SerializationInfo info, object obj)
        {
            Type     objType  = obj.GetType();
            Assembly assembly = Assembly.GetAssembly(typeof(ModuleUtils));

            info.AssemblyName = assembly.GetName().Name;
            info.FullTypeName = $"{objType.Namespace}.{objType.Name}";

            foreach (PropertyInfo propertyInfo in objType.GetProperties())
            {
                RuntimeSerializeIgnoreAttribute ignoreAttribute =
                    propertyInfo.GetCustomAttribute <RuntimeSerializeIgnoreAttribute>();
                if (null != ignoreAttribute && ignoreAttribute.Ignore)
                {
                    continue;
                }
                object value = propertyInfo.GetValue(obj);
                if (null == value)
                {
                    continue;
                }
                Type propertyType = propertyInfo.PropertyType;
                // 如果是类类型且不为null,则获取真实的类型
                if (propertyType.IsClass)
                {
                    propertyType = value.GetType();
                }
                info.AddValue(propertyInfo.Name, value, propertyType);
            }
        }
Ejemplo n.º 2
0
        public static void FillDeserializationInfo(SerializationInfo info, object obj, Type type)
        {
            SerializationInfoEnumerator infoEnumerator = info.GetEnumerator();

            while (infoEnumerator.MoveNext())
            {
                PropertyInfo propertyInfo = type.GetProperty(infoEnumerator.Current.Name, BindingFlags.Instance | BindingFlags.Public);
                RuntimeSerializeIgnoreAttribute ignoreAttribute =
                    propertyInfo.GetCustomAttribute <RuntimeSerializeIgnoreAttribute>();
                if (null != ignoreAttribute && ignoreAttribute.Ignore)
                {
                    continue;
                }
                Type propertyType = propertyInfo.PropertyType;
                // 如果包含RuntimeType属性则说明该属性使用了某个指定的实现类型而非当前属性的类型
                RuntimeTypeAttribute runtimeType;
                if (null != (runtimeType = propertyInfo.GetCustomAttribute <RuntimeTypeAttribute>()))
                {
                    propertyType = runtimeType.RealType;
                }

                //如果包含该属性说明是集合类型,需要特殊处理
                GenericCollectionAttribute genericAttribute = propertyType.GetCustomAttribute <GenericCollectionAttribute>();

                if (null == genericAttribute)
                {
                    object propertyValue = info.GetValue(propertyInfo.Name, propertyType);
                    if (null != propertyValue)
                    {
                        propertyInfo.SetValue(obj, propertyValue);
                    }
                }
                else
                {
                    Type elementType = GetRawGenericElementType(propertyType);
                    if (null == elementType)
                    {
                        throw new InvalidOperationException();
                    }
                    const string addMethodName = "Add";

                    MethodInfo addMethod = propertyType.GetMethod(addMethodName, BindingFlags.Instance | BindingFlags.Public, null,
                                                                  CallingConventions.Standard, new Type[] { elementType }, new ParameterModifier[0]);
                    // 构造类型为List<RealType>的临时集合去获取json的值
                    Type   typeCollection    = typeof(List <>);
                    Type   tmpCollectionType = typeCollection.MakeGenericType(genericAttribute.GenericType);
                    object tmpCollection     = info.GetValue(propertyInfo.Name, tmpCollectionType);

                    object propertyValue = null;
                    if (null != tmpCollection)
                    {
                        ConstructorInfo propertyConstructor =
                            propertyType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, new Type[0], null);
                        propertyValue = propertyConstructor.Invoke(new object[0]);
                        foreach (object item in tmpCollection as IEnumerable)
                        {
                            addMethod.Invoke(propertyValue, new object[] { item });
                        }
                    }
                    propertyInfo.SetValue(obj, propertyValue);
                }
            }
        }