예제 #1
0
        private static object ReflectIn(StageElement element)
        {
            object obj;
            object obj1;
            object obj2;
            string str = element.AttributeValueOrDefault <string>("type", null);

            if (str == null)
            {
                throw new SerializationException("Invalid structure detected, missing type info.");
            }
            Type type = Type.GetType(str, true);

            try
            {
                obj = Activator.CreateInstance(type, true);
            }
            catch (MissingMethodException missingMethodException1)
            {
                MissingMethodException missingMethodException = missingMethodException1;
                throw new SerializationException(string.Format("Unable to create type {0}, ensure it has a parameterless constructor", type.Name), missingMethodException);
            }
            IInitializeAfterDeserialization initializeAfterDeserialization = obj as IInitializeAfterDeserialization;

            if (initializeAfterDeserialization != null)
            {
                if (SerializationMaster._requiresInit == null)
                {
                    throw new InvalidOperationException("An entity requires initialization but was unable to register, call UnstageAndInitialize instead.");
                }
                SerializationMaster._requiresInit.Add(initializeAfterDeserialization);
            }
            foreach (PropertyInfo serializedProperty in SerializationMaster.GetSerializedProperties(type))
            {
                StageItem stageItem = element.Item(serializedProperty.Name);
                if (stageItem == null || !SerializationMaster.TryUnstage(stageItem, serializedProperty.PropertyType, out obj1))
                {
                    continue;
                }
                serializedProperty.SetValue(obj, obj1, null);
            }
            foreach (FieldInfo serializedField in SerializationMaster.GetSerializedFields(type))
            {
                StageItem stageItem1 = element.Item(serializedField.Name);
                if (stageItem1 == null || !SerializationMaster.TryUnstage(stageItem1, serializedField.FieldType, out obj2))
                {
                    continue;
                }
                serializedField.SetValue(obj, obj2);
            }
            return(obj);
        }