当前位置: 首页>>代码示例>>C#>>正文


C# SerializationInfo.GetType方法代码示例

本文整理汇总了C#中System.Runtime.Serialization.SerializationInfo.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# SerializationInfo.GetType方法的具体用法?C# SerializationInfo.GetType怎么用?C# SerializationInfo.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.Serialization.SerializationInfo的用法示例。


在下文中一共展示了SerializationInfo.GetType方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Deserialize

        /// <summary>
        /// Deserialize to a specific type.
        /// This call bypasses the call to ObjectType(data)
        /// and uses the object type "t".
        /// </summary>
        /// <param name="s">Stream to deserialize</param>
        /// <param name="t">Object type to be created</param>
        /// <returns></returns>
        public Object Deserialize(Stream s, Type t)
        {
            Object ret = null;
            // read one line from the stream that defines our object
            m_FirstLine = false;
            if (s.CanRead && s.Position == 0)
            {
                m_FirstLine = true;
            }
            String str = ReadLine(s);

            // local storage of the elements read from the line
            Dictionary<String, String> data
                = new Dictionary<string, string>();

            DeserializeFromString(str, m_EscapeChar
                , m_NameDelimiter, m_ValueDelimiter, ref data);
            try
            {
                if (m_StorageType == StorageType.CSV
                    && UseFirstLineAsColumnNames && m_FirstLine)
                { // create name map for this stream
                    foreach (KeyValuePair<String, String> value in data)
                    {
                        m_FirstLineColumnNames.Add(value.Value);
                    }
                    // return a copy of the name list
                    t = m_FirstLineColumnNames.GetType();
                    Type[] types = { };
                    Object[] objs = { };
                    ret = t.GetConstructor(types).Invoke(objs);
                }
                else if (t != typeof(Object))
                {
                    // create a different SerializationInfo for each object
                    SerializationInfo info
                        = new SerializationInfo(t, m_Converter);
                    // add the name/value pairs to the SerializationInfo object
                    if (m_StorageType == StorageType.NameValue)
                    {
                        foreach (KeyValuePair<String, String> value in data)
                        {
                            info.AddValue(value.Key, value.Value);
                        }
                    }
                    else if (m_StorageType == StorageType.ValueOnly
                        || m_StorageType == StorageType.CSV)
                    {
                        List<String> nameMap = ObjectNameMap(t);
                        IEnumerator<String> nameItr = null;
                        if (nameMap != null)
                        {
                            // starts before first item - MoveNext() to set at first item
                            nameItr = nameMap.GetEnumerator();
                        }
                        foreach (KeyValuePair<String, String> value in data)
                        {
                            if (nameMap != null)
                            {
                                String key = String.Empty;
                                if (nameItr.MoveNext())
                                {
                                    if (nameItr.Current != null)
                                    {
                                        key = nameItr.Current;
                                    }
                                }
                                // an empty key from the name map
                                // indicates to use the auto-generated key
                                if (key.Length > 0)
                                {
                                    info.AddValue(key, value.Value);
                                }
                                else
                                {
                                    info.AddValue(value.Key, value.Value);
                                }
                            }
                            else
                            { // use the auto-generated key
                                info.AddValue(value.Key, value.Value);
                            }
                        }
                    }

                    // call private constructor for ISerializable objects
                    Type[] types = { info.GetType(), m_Context.GetType() };
                    Object[] objs = { info, m_Context };
                    ret = t.GetConstructor(types).Invoke(objs);
                }
                else
                {
//.........这里部分代码省略.........
开发者ID:jimbosoft,项目名称:CommonLib,代码行数:101,代码来源:TextFormatter.cs

示例2: Deserialize

        /// <summary>
        /// Deserialize to a specific type.
        /// This call bypasses the call to ObjectType(data)
        /// and uses the object type "t".
        /// </summary>
        /// <param name="s">Stream to deserialize</param>
        /// <param name="t">Object type to be created</param>
        /// <returns></returns>
        public object Deserialize(Stream s, Type t)
        {
            object ret = null;
            // read one line from the stream that defines our object
            _mFirstLine = s.CanRead && s.Position == 0;
            string str = ReadLine(s);

            // local storage of the elements read from the line
            Dictionary<string, string> data
                = new Dictionary<string, string>();

            DeserializeFromString(str, Escape
                , NameDelimiter, ValueDelimiter, ref data);
            try
            {
                if (TextStorage == TextStorageType.Csv
                    && UseFirstLineAsColumnNames && _mFirstLine)
                { // create name map for this stream
                    foreach (KeyValuePair<string, string> value in data)
                    {
                        _mFirstLineColumnNames.Add(value.Value);
                    }
                    // return a copy of the name list
                    t = _mFirstLineColumnNames.GetType();
                    Type[] types = { };
                    object[] objs = { };
                    var constructorInfo = t.GetConstructor(types);
                    if (constructorInfo != null) ret = constructorInfo.Invoke(objs);
                }
                else if (t != typeof(object))
                {
                    // create a different SerializationInfo for each object
                    SerializationInfo info
                        = new SerializationInfo(t, _mConverter);
                    // add the name/value pairs to the SerializationInfo object
                    if (TextStorage == TextStorageType.NameValue)
                    {
                        foreach (KeyValuePair<string, string> value in data)
                        {
                            info.AddValue(value.Key, value.Value);
                        }
                    }
                    else if (TextStorage == TextStorageType.ValueOnly
                        || TextStorage == TextStorageType.Csv)
                    {
                        List<string> nameMap = ObjectNameMap(t);
                        IEnumerator<string> nameItr = null;
                        if (nameMap != null)
                        {
                            // starts before first item - MoveNext() to set at first item
                            nameItr = nameMap.GetEnumerator();
                        }
                        foreach (KeyValuePair<string, string> value in data)
                        {
                            if (nameMap != null)
                            {
                                string key = string.Empty;
                                if (nameItr.MoveNext())
                                {
                                    if (nameItr.Current != null)
                                    {
                                        key = nameItr.Current;
                                    }
                                }
                                // an empty key from the name map
                                // indicates to use the auto-generated key
                                info.AddValue(key.Length > 0 ? key : value.Key, value.Value);
                            }
                            else
                            { // use the auto-generated key
                                info.AddValue(value.Key, value.Value);
                            }
                        }
                    }

                    // call private constructor for ISerializable objects
                    Type[] types = { info.GetType(), _mContext.GetType() };
                    object[] objs = { info, _mContext };
                    var constructorInfo = t.GetConstructor(types);
                    if (constructorInfo != null) ret = constructorInfo.Invoke(objs);
                }
                else
                {
                    // no type defined by the deserialized string
                }
            }
            catch (SerializationException e)
            {
                throw new SerializationException(nameof(Deserialize) + "(Stream, Type)", e);
            }
            // return instance of new object
            return ret;
//.........这里部分代码省略.........
开发者ID:devworker55,项目名称:Mammatus,代码行数:101,代码来源:TextFormatter.cs

示例3: Map

        protected Map(SerializationInfo info, StreamingContext context)
        {
            source = info.GetString("source");
              sourceType = (MapSourceType)(info.GetValue("sourceType", typeof(MapSourceType)));
              storageType = (MapStorageType)(info.GetValue("storageType", typeof(MapStorageType)));
              switch (storageType)
              {
            case MapStorageType.Inline:
              //rawData = (byte[])(info.GetValueNo("rawData", typeof(byte[])));
              var getValueNoThrowMethod = info.GetType().GetMethod("GetValueNoThrow", BindingFlags.Instance | BindingFlags.NonPublic);
              rawData = (byte[])getValueNoThrowMethod.Invoke(info, new object[] { "rawData", typeof(byte[]) });

              if (rawData != null)
              {
            // version 2.3 file format contains rawData field, create image from it
            using (var ms = new MemoryStream(rawData))
            {
              image = (Bitmap)System.Drawing.Image.FromStream(ms);
            }
              }
              else
              {
            // version 2.2 file format
            image = (Bitmap)(info.GetValue("image", typeof(Bitmap)));
              }
              break;

            case MapStorageType.Reference:
              switch (sourceType)
              {
            case MapSourceType.FileSystem:
              image = (Bitmap)System.Drawing.Image.FromFile(source);
              break;
            case MapSourceType.Url:
              image = GetImageFromUrl(source);
              break;
              }
              break;
              }
        }
开发者ID:jarkko,项目名称:quickroute-gps,代码行数:40,代码来源:Map.cs

示例4: GetTypeInstanceProxy

            private STypeInstanceProxy GetTypeInstanceProxy(object o, Type nullableType)
            {
                STypeInstanceProxy result = null;

                if (o != null)
                {
                    Type objectType = nullableType == null ? o.GetType() : nullableType;
                    if (!objectType.IsSerializable && !objectType.IsNested && !objectType.IsEnum)
                    {
                        throw new SerializationException("Type has not been marked as serializable.");
                    }

                    STypeInfo ti = GetTypeInfo(objectType);

                    bool firstTime = false;
                    long instanceId = mGenerator.GetId(o, out firstTime);
                    if (!firstTime)
                    {
                        result = mReferenceVsTypeInstances[instanceId];
                    }

                    if (result == null)
                    {
                        result = (STypeInstanceProxy)ti.Clone();

                        result.InstanceId = instanceId; //mProxyIdGenerator;
                        result.InstanceValue = o;
                        mReferenceVsTypeInstances.Add(result.InstanceId, result);

                        if (mSystemTypes.Contains(objectType))
                        {
                        }
                        else if (objectType.IsEnum)
                        {
                        }
                        else if (o is ISerializable)
                        {
                            // serializable type
                            ISurrogateSelector selector = null;
                            ISerializationSurrogate surrogate = (Selector == null ? null : Selector.GetSurrogate(objectType, this.Context, out selector));

                            SerializationInfo info = new SerializationInfo(ti.Type, new FormatterConverter());
                            if (surrogate == null)
                            {
                                ISerializable serializable = (ISerializable)o;
                                serializable.GetObjectData(info, this.Context);
                            }
                            else
                            {
                                surrogate.GetObjectData(o, info, this.Context);
                            }

                            result.SerializationInfo = info;
                            if (info.MemberCount > 0)
                            {
                                result.ArrayKeys = new List<STypeInstanceProxy>();
                                result.ArrayItems = new List<STypeInstanceProxy>();
                                string[] memberNames = (string[])info.GetType().GetProperty("MemberNames", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetGetMethod(true).Invoke(info, null);
                                object[] memberValues = (object[])info.GetType().GetProperty("MemberValues", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetGetMethod(true).Invoke(info, null);

                                for (int i = 0; i < memberNames.Length; i++)
                                {
                                    string memberName = memberNames[i];
                                    if (memberName != null)
                                    {
                                        object memberValue = memberValues[i];
                                        result.ArrayKeys.Add(GetTypeInstanceProxy(memberName, null));
                                        result.ArrayItems.Add(GetTypeInstanceProxy(memberValue, null));
                                    }
                                }
                            }
                        }
                        else if (objectType.IsGenericType && objectType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                        {
                            // a reflector nem viszi a Nullable típust
                            dynamic dn = o;
                            result.FieldVsProxy.Add("value", GetTypeInstanceProxy(dn, dn.GetType()));
                        }
                        else if (objectType.IsArray && objectType.GetArrayRank() == 1 && objectType.Equals(typeof(byte[])))
                        {
                            // a byte tömböt máshogy serializálom
                        }
                        else if (objectType.IsArray && objectType.GetArrayRank() == 1)
                        {
                            Array array = o as Array;
                            List<STypeInstanceProxy> items = new List<STypeInstanceProxy>();
                            result.ArrayItems = items;
                            CollectArrayItems(items, array, new long[objectType.GetArrayRank()]);
                        }
                        else if (objectType.IsArray && objectType.GetArrayRank() > 1)
                        {
                            // multidim array
                            Array array = o as Array;
                            List<STypeInstanceProxy> items = new List<STypeInstanceProxy>();
                            result.ArrayItems = items;
                            CollectMultiArrayItems(items, array, 0, new long[objectType.GetArrayRank()]);
                        }
                        else
                        {
                            while (objectType != typeof(object) && objectType != typeof(ValueType) &&
//.........这里部分代码省略.........
开发者ID:JZO001,项目名称:Forge,代码行数:101,代码来源:BinarySerializer.cs


注:本文中的System.Runtime.Serialization.SerializationInfo.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。