Пример #1
0
            public void SetObjectData(UnitySerializationInfo info)
            {
                var cnt = info.GetInt32("count");

                _elementType = TypeUtil.ParseType(info.GetString("assembly"), info.GetString("type"));
                _isArray     = info.GetBoolean("isArray");

                if (_isArray)
                {
                    var arr = System.Array.CreateInstance(_elementType, cnt);
                    for (int i = 0; i < cnt; i++)
                    {
                        arr.SetValue(info.GetValue("element" + i.ToString(), _elementType), i);
                    }
                    _lst = arr as System.Collections.IList;
                }
                else
                {
                    var lstType    = typeof(List <>);
                    var genLstType = lstType.MakeGenericType(_elementType);
                    var lst        = System.Activator.CreateInstance(genLstType) as System.Collections.IList;
                    for (int i = 0; i < cnt; i++)
                    {
                        lst.Add(info.GetValue("element" + i.ToString(), _elementType));
                    }
                    _lst = lst;
                }
            }
        public object SetObjectData(object obj, System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISurrogateSelector selector)
        {
            _unitySerialInfo.StartContext(info, context);

            if (obj is IUnitySerializable)
            {
                (obj as IUnitySerializable).SetObjectData(_unitySerialInfo);
            }
            else
            {
                var tp = obj.GetType();
                foreach (var fi in GetAllSerializableFields(tp))
                {
                    try
                    {
                        var value = _unitySerialInfo.GetValue(fi.Name, fi.FieldType);
                        fi.SetValue(obj, value);
                    }
                    catch
                    {
                        //do nothing
                    }
                }
            }
            _unitySerialInfo.EndContext();

            return(obj);
        }