Ejemplo n.º 1
0
        /// <summary>
        /// Initialisiert eine neue Instanz der class <see cref="TypeSerializer"/> für das
        /// nicht serialisierbare Objekt <paramref name="instance"/> für eine Serialisierung mittles
        /// Reflection.
        /// </summary>
        /// <param name="instance">The instance.</param>
        private TypeSerializer(object instance)
        {
            using (var log = new EnterExitLogger(s_log, "instance = {0}", instance)) {
                m_instance     = instance;
                m_type         = m_instance.GetType();
                m_fields       = new FieldSerializerDict();
                m_constructors = new ConstructorReflectorDict();

                foreach (var ci in m_type.GetConstructors(MemberReflector.AllInstanceDeclared))
                {
                    var ctor = new ConstructorReflector(ci);
                    m_constructors.Add(TypeReflector.BuildMethodSignature(ci), ctor);
                    if (ctor.IsDefaultConstructor)
                    {
                        m_defaultConstructor = ctor;
                    }
                }

                var fields = m_type.GetFields(MemberReflector.AllInstanceDeclared);

                foreach (var fi in fields)
                {
                    var fieldValue = fi.GetValue(m_instance);
                    fieldValue = CreateSerializerWrapper(fieldValue);

                    m_fields.Add(fi.Name, new FieldSerializer(fi, fieldValue));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserializes die angegebenen instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        private object Deserialize(object instance)
        {
            using (var log = new EnterExitLogger(s_log, "instance = {0}, type = {1}", instance, m_type)) {
                if (instance == null)
                {
                    if (DefaultConstructor != null)
                    {
                        instance = DefaultConstructor.Invoke(null);
                    }
                    else
                    {
                        if (m_type.IsArray)
                        {
                            var elementType = m_type.GetElementType();

                            var arrayCtor = GetConstructor(".ctor(System.Int32)");

                            //
                            // Dummy: Array mit einem Element anlegen
                            //
                            var array = (object[])arrayCtor.Invoke(new object[] { 1 });

                            if (!elementType.IsAbstract)
                            {
                                //ConstructorReflector elCtor = new ConstructorReflector(elementType, MemberReflector.AllInstanceDeclared);
                                //object element = elCtor.Invoke(null);
                                //array[0] = element;
                            }

                            instance = array;
                        }
                        else if (m_type == typeof(LogicalMethodInfo))
                        {
                            //
                            // Achtung: Spezialbehandlung, da die Klasse LogicalMethodInfo nur einen Konstruktor
                            // mit einem MethodInfo-Parameter hat! -> wir erzeugen einen Dummy-Konstruktorparameter;
                            // die konkreten Member werden bei der Deserialisierung gesetzt.
                            //
                            var tr = new TypeReflector(typeof(DummyInfoWebService));
                            var mi = typeof(DummyInfoWebService).GetMethod("getString");

                            //var rtmiCtor = new ConstructorReflector(Type.GetType("System.Reflection.RuntimeMethodInfo"));
                            //var rtmi = rtmiCtor.Invoke(null);

                            var ctorSignature = TypeReflector.BuildMethodSignature(".ctor", new Type[] { Type.GetType("System.Reflection.MethodInfo") });
                            var ctor          = GetConstructor(ctorSignature);
                            instance = ctor.Invoke(new object[] { mi });
                        }
                        else
                        {
                            throw new InvalidOperationException(string.Format("Type {0} has no default constructor", m_type));
                        }
                    }
                }

                //
                // Deseralisierung der Member
                //
                foreach (var field in m_fields.Values)
                {
                    var fieldValue = field.Value;

                    var fieldSerializer = field.Value as TypeSerializer;

                    if (fieldSerializer != null)
                    {
                        var instanceFieldValue = fieldSerializer.Deserialize();
                        field.SetValue(instance, instanceFieldValue);
                    }
                    else if (field.Value != null)
                    {
                        if (field.Value is Hashtable)
                        {
                            var ht = new Hashtable();
                            foreach (DictionaryEntry de in ((Hashtable)field.Value))
                            {
                                var key   = de.Key;
                                var value = de.Value;

                                if (key is TypeSerializer)
                                {
                                    key = ((TypeSerializer)key).Deserialize();
                                }

                                if (value is TypeSerializer)
                                {
                                    value = ((TypeSerializer)value).Deserialize();
                                }

                                ht.Add(key, value);
                            }

                            field.SetValue(instance, ht);
                        }
                        else if (field.Info.FieldType.IsArray)
                        {
                            var fieldValues = (object[])field.Value;

                            var ctor = new ConstructorReflector(field.Info.FieldType, new Type[] { typeof(int) }, MemberReflector.AllInstanceDeclared);

                            var array = (object[])ctor.Invoke(new object[] { fieldValues.Length });
                            for (var i = 0; i < fieldValues.Length; i++)
                            {
                                if (fieldValues[i] is TypeSerializer)
                                {
                                    array[i] = ((TypeSerializer)fieldValues[i]).Deserialize();
                                }
                                else
                                {
                                    array[i] = fieldValues[i];
                                }
                            }

                            field.SetValue(instance, array);
                        }
                        else
                        {
                            // regular field
                            field.SetValue(instance, field.Value);
                        }
                    }
                }

                return(instance);
            }
        }