Exemple #1
0
        public Object GetValue(String name, Type type)
        {
            int    index;
            Object value;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            index = names.IndexOf(name);
            if (index != -1)
            {
                value = values[index];
                if (value != null)
                {
                    if (type.IsAssignableFrom((Type)(types[index])))
                    {
                        return(value);
                    }
                    else
                    {
                        return(converter.Convert(value, type));
                    }
                }
            }
            return(null);
        }
Exemple #2
0
        //
        // The user should call one of these getters to get the data back in the
        // form requested.
        //

        public Object GetValue(String name, Type type)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            Contract.EndContractBlock();

            RuntimeType rt = type as RuntimeType;

            if (rt == null)
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType);
            }

            Type   foundType;
            Object value;

            value = GetElement(name, out foundType);

            if (Object.ReferenceEquals(foundType, type) || type.IsAssignableFrom(foundType) || value == null)
            {
                return(value);
            }

            Debug.Assert(m_converter != null, "[SerializationInfo.GetValue]m_converter!=null");

            return(m_converter.Convert(value, type));
        }
        public object GetValue(string name, Type type)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            SerializationEntry entry;

            if (!entries.TryGetValue(name, out entry))
            {
                throw new SerializationException(String.Format("The member '{0}' was not added.", name));
            }

            var value = entry.Value;

#if !PCL
            if (value != null && !type.IsInstanceOfType(value))
            {
                value = converter.Convert(value, type);
            }
#else
            if (value != null && !type.IsTypeOf(value))
            {
                value = converter.Convert(value, type);
            }
#endif
            return(value);
        }
Exemple #4
0
        //
        // The user should call one of these getters to get the data back in the
        // form requested.
        //

        public Object GetValue(String name, Type type)
        {
            throw new NotImplementedException();

            Type   foundType;
            Object value;

            if (null == type)
            {
                throw new ArgumentNullException("type");
            }

//      value = GetElement(name, out foundType);
//      if (RemotingServices.IsTransparentProxy(value))
//      {
//        RealProxy proxy = RemotingServices.GetRealProxy(value);
//        if (RemotingServices.ProxyCheckCast(proxy, type))
//          return value;
//      }
//      else if (foundType == type || type.IsAssignableFrom(foundType) || value == null)
//      {
//        return value;
//      }

            Debug.Assert(m_converter != null, "[SerializationInfo.GetValue]m_converter!=null");

            return(m_converter.Convert(value, type));
        }
Exemple #5
0
        /// <summary>
        /// 读取一个值对象
        /// </summary>
        /// <param name="reader"> </param>
        /// <param name="jsonType"> </param>
        /// <returns> </returns>
        private object ReadValue(UnsafeJsonReader reader, JsonType jsonType)
        {
            reader.CheckEnd();
            switch (reader.Current)
            {
            case '[':
                reader.MoveNext();
                var array = ReadList(reader, jsonType);
                reader.SkipChar(']', true);
                return(array);

            case '{':
                reader.MoveNext();
                var obj = ReadObject(reader, jsonType);
                reader.SkipChar('}', true);
                return(obj);

            case '"':
            case '\'':
                return(ParseString(reader, jsonType));

            default:
                var val = reader.ReadConsts(false);
                if (_converter != null)
                {
                    return(_converter.Convert(val, jsonType.TypeCode));
                }
                return(jsonType.Convert(val, jsonType.Type));
            }
        }
Exemple #6
0
 public object Convert(object value, Type type)
 {
     try
     {
         return(_impl.Convert(value, type));
     }
     catch (InvalidCastException e)
     {
         throw new InvalidCastException(
                   $"Failed to convert the object {value} to {type}.",
                   e
                   );
     }
 }
Exemple #7
0
        private /*static*/ object readValue(XmlReader xmlReader, Type type, IFormatterConverter converter)
        {
            System.Diagnostics.Debug.Assert(null != xmlReader, "The 'xmlReader' argument cannot be null.");
            System.Diagnostics.Debug.Assert(null != type, "The 'type' argument cannot be null.");
            System.Diagnostics.Debug.Assert(this.isSimpleType(type), "The Type type is not a simple type.");
            System.Diagnostics.Debug.Assert(null != converter, "The 'converter' argument cannot be null.");

            object value = null;             // Return value
            string valueRepresentation = xmlReader.ReadString();

            if (type.IsPrimitive || typeof(System.String) == type)
            {
                value = converter.Convert(valueRepresentation, type);
            }
            else if (type.IsEnum)
            {
                value = Enum.Parse(type, valueRepresentation);
            }
            else if (typeof(System.Guid) == type)
            {
                value = new GuidConverter().ConvertFromInvariantString(valueRepresentation);
            }

            // When the value is the empty string the xml element is empty and there is not an xml end element.
            // Read the following element.
            if (xmlReader.IsEmptyElement)
            {
                xmlReader.ReadStartElement();
            }

            return(value);
        }
Exemple #8
0
        public object GetValue(string name, Type type)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name is null.");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!serialized.ContainsKey(name))
            {
                throw new SerializationException("No element named " + name + " could be found.");
            }

            SerializationEntry entry = (SerializationEntry)serialized[name];

            if (entry.Value != null && !type.IsInstanceOfType(entry.Value))
            {
                return(converter.Convert(entry.Value, type));
            }
            else
            {
                return(entry.Value);
            }
        }
Exemple #9
0
        /// <summary>
        /// Extends Convert so that methods that return a specific type object given a Type parameter can be
        /// used as generic method and casting is not required.
        /// <example>
        /// iformatterconverter.Convert&lt;int&gt;(value);
        /// </example>
        /// </summary>
        public static T Convert <T>(this IFormatterConverter iformatterconverter, Object value)
        {
            if (iformatterconverter == null)
            {
                throw new ArgumentNullException("iformatterconverter");
            }

            return((T)iformatterconverter.Convert(value, typeof(T)));
        }
        public object GetValue(string name, Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            Type   foundType;
            object value;

            value = GetElement(name, out foundType);
            if (ReferenceEquals(foundType, type) || type.IsAssignableFrom(foundType) || value == null)
            {
                return(value);
            }

            Debug.Assert(_converter != null);
            return(_converter.Convert(value, type));
        }
Exemple #11
0
 protected override void Try(string arg, out object value)
 {
     try
     {
         value = _converter.Convert(arg, ParameterType);
     }
     catch
     {
         value = null;
     }
 }
Exemple #12
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public Object GetValue(String name, Type type)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException("type");
            }
            Contract.EndContractBlock();

            RuntimeType rt = type as RuntimeType;

            if (rt == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
            }

            Type   foundType;
            Object value;

            value = GetElement(name, out foundType);
#if FEATURE_REMOTING
            if (RemotingServices.IsTransparentProxy(value))
            {
                RealProxy proxy = RemotingServices.GetRealProxy(value);
                if (RemotingServices.ProxyCheckCast(proxy, rt))
                {
                    return(value);
                }
            }
            else
#endif
            if (Object.ReferenceEquals(foundType, type) || type.IsAssignableFrom(foundType) || value == null)
            {
                return(value);
            }

            Contract.Assert(m_converter != null, "[SerializationInfo.GetValue]m_converter!=null");

            return(m_converter.Convert(value, type));
        }
        public object?GetValue(string name, Type type)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (!type.IsRuntimeImplemented())
            {
                throw new ArgumentException(SR.Argument_MustBeRuntimeType);
            }

            Type   foundType;
            object?value = GetElement(name, out foundType);

            if (ReferenceEquals(foundType, type) || type.IsAssignableFrom(foundType) || value == null)
            {
                return(value);
            }

            Debug.Assert(_converter != null, "[SerializationInfo.GetValue]_converter!=null");
            return(_converter.Convert(value, type));
        }
Exemple #14
0
 /// <summary>
 /// 转换对象到指定的类型,如果失败返回默认值
 /// </summary>
 public static bool TryConvert(this IFormatterConverter converter, object value, Type type, out object result)
 {
     if (converter is IFormatterConverterExtra extra)
     {
         return(extra.TryConvert(value, out result));
     }
     try
     {
         result = converter.Convert(value, type);
         return(true);
     }
     catch
     {
         result = null;
         return(false);
     }
 }
 protected override Exception Try(string arg, out object value)
 {
     try
     {
         value = _converter.Convert(arg, Type);
         return(null);
     }
     catch
     {
         value = null;
         if (_propertyCount > 0 && arg.Length == 0)
         {
             return(new ApiException(ExceptionCode.ParameterMissing, FullName));
         }
         return(new ApiException(ExceptionCode.ParameterFormatError, FullName, TypeName));
     }
 }
Exemple #16
0
 /// <summary>
 /// 转换对象到指定的类型,如果失败返回默认值
 /// </summary>
 public static bool TryConvert <T>(this IFormatterConverter converter, object value, out T result)
 {
     if (converter is IFormatterConverterExtra extra)
     {
         return(extra.TryConvert <T>(value, out result));
     }
     try
     {
         result = (T)converter.Convert(value, typeof(T));
         return(true);
     }
     catch
     {
         result = default;
         return(false);
     }
 }
Exemple #17
0
        /// <summary>
        /// Determines the value of an object.
        /// </summary>
        /// <param name="reader">The XML reader the read from.</param>
        /// <param name="converter">The converter used to parse the values from the XML.</param>
        /// <param name="objectType">The type of the object to create.</param>
        /// <returns>The value of the object.</returns>
        private object DetermineValue(XmlReader reader, IFormatterConverter converter, Type objectType)
        {
            object parsedObject;

            // check if the value can be directly determined or that the type is a complex type.
            if (objectType.IsPrimitive || objectType == typeof(string) || objectType.IsEnum || objectType == typeof(DateTime) || objectType == typeof(object))
            {
                // directly parse
                parsedObject = converter.Convert(reader.ReadString(), objectType);
            }
            else
            {
                // Initialize the object (recursive call)
                parsedObject = InitializeObject(reader, converter, objectType);
            }

            return(parsedObject);
        }
		private /*static*/ object readValue(XmlReader xmlReader, Type type, IFormatterConverter converter)
		{
			System.Diagnostics.Debug.Assert(null != xmlReader, "The 'xmlReader' argument cannot be null.");
			System.Diagnostics.Debug.Assert(null != type, "The 'type' argument cannot be null.");
			System.Diagnostics.Debug.Assert(this.isSimpleType(type), "The Type type is not a simple type.");
			System.Diagnostics.Debug.Assert(null != converter, "The 'converter' argument cannot be null.");

			object value = null; // Return value			
			string valueRepresentation = xmlReader.ReadString();
			if (type.IsPrimitive || typeof(System.String) == type) {
				value = converter.Convert(valueRepresentation, type);
			}
			else if (type.IsEnum) {
				value = Enum.Parse(type, valueRepresentation);
			}
			else if (typeof(System.Guid) == type) {
				value = new GuidConverter().ConvertFromInvariantString(valueRepresentation);
			}

			// When the value is the empty string the xml element is empty and there is not an xml end element.
			// Read the following element.
			if (xmlReader.IsEmptyElement)
				xmlReader.ReadStartElement();

			return value;
		}
Exemple #19
0
 public object Convert(object value, Type type)
 {
     return(_impl.Convert(value, type));
 }