ConvertFromInvariantString() 공개 메소드

public ConvertFromInvariantString ( ITypeDescriptorContext context, string text ) : object
context ITypeDescriptorContext
text string
리턴 object
        private static bool InnerConvertObjectToType(object obj, Type type, bool throwOnError,
                                                     out object convertedObject)
        {
            Type originalType = obj.GetType();
            OriginalTypeConverter converter = TypeDescriptor.GetConverter(type);

            if (converter.CanConvertFrom(originalType))
            {
                try
                {
                    convertedObject = converter.ConvertFrom(null, CultureInfo.InvariantCulture, obj);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }

                    convertedObject = null;
                    return(false);
                }
            }

            if (converter.CanConvertFrom(typeof(string)) && !(obj is DateTime))
            {
                try
                {
                    string text = TypeDescriptor.GetConverter(originalType).ConvertToInvariantString(obj);

                    convertedObject = converter.ConvertFromInvariantString(text);
                    return(true);
                }
                catch
                {
                    if (throwOnError)
                    {
                        throw;
                    }

                    convertedObject = null;
                    return(false);
                }
            }

            if (type.IsInstanceOfType(obj))
            {
                convertedObject = obj;
                return(true);
            }

            if (throwOnError)
            {
                throw new InvalidOperationException();
            }

            convertedObject = null;
            return(false);
        }
예제 #2
0
        /// <summary>
        /// 使用指定的上下文和区域性信息将给定的对象转换为此转换器的类型。
        /// </summary>
        /// <param name="context"><see cref="T:System.ComponentModel.ITypeDescriptorContext" />,提供格式上下文。</param>
        /// <param name="culture">用作当前区域性的 <see cref="T:System.Globalization.CultureInfo" />。</param>
        /// <param name="value">要转换的 <see cref="T:System.Object" />。</param>
        /// <returns>
        /// 表示转换的 value 的 <see cref="T:System.Object" />。
        /// </returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string   _input = (string)value;
                string[] _items = string.IsNullOrEmpty(_input) ? new string[0] : _input.Split(';').Select(x => x.Trim()).ToArray();

                var _result = new Dictionary <K, V>();
                Array.ForEach(_items, s =>
                {
                    string[] _keyValueStr = string.IsNullOrEmpty(s) ? new string[0] : s.Split(',').Select(x => x.Trim()).ToArray();
                    if (_keyValueStr.Length == 2)
                    {
                        object _dictionaryKey   = (K)typeConverterKey.ConvertFromInvariantString(_keyValueStr[0]);
                        object _dictionaryValue = (V)typeConverterValue.ConvertFromInvariantString(_keyValueStr[1]);
                        if (_dictionaryKey != null && _dictionaryValue != null)
                        {
                            if (!_result.ContainsKey((K)_dictionaryKey))
                            {
                                _result.Add((K)_dictionaryKey, (V)_dictionaryValue);
                            }
                        }
                    }
                });

                return(_result);
            }
            return(base.ConvertFrom(context, culture, value));
        }
예제 #3
0
        /// <summary>
        /// 使用指定的上下文和区域性信息将给定的对象转换为此转换器的类型。
        /// </summary>
        /// <param name="context"><see cref="T:System.ComponentModel.ITypeDescriptorContext" />,提供格式上下文。</param>
        /// <param name="culture">用作当前区域性的 <see cref="T:System.Globalization.CultureInfo" />。</param>
        /// <param name="value">要转换的 <see cref="T:System.Object" />。</param>
        /// <returns>
        /// 表示转换的 value 的 <see cref="T:System.Object" />。
        /// </returns>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] _items  = GetStringArray((string)value);
                var      _result = new List <T>();
                Array.ForEach(_items, s =>
                {
                    object _item = typeConverter.ConvertFromInvariantString(s);

                    if (_item != null)
                    {
                        _result.Add((T)_item);
                    }
                });
                return(_result);
            }

            return(base.ConvertFrom(context, culture, value));
        }
 private bool ReadValue(XmlNode node, TypeConverter converter, ArrayList errors, out object value)
 {
     try
     {
         foreach (XmlNode child in node.ChildNodes)
         {
             if (child.NodeType == XmlNodeType.Text)
             {
                 value = converter.ConvertFromInvariantString(node.InnerText);
                 return true;
             }
             if (child.Name.Equals("Binary"))
             {
                 byte[] data = Convert.FromBase64String(child.InnerText);
                 if (this.GetConversionSupported(converter, typeof(byte[])))
                 {
                     value = converter.ConvertFrom(null, CultureInfo.InvariantCulture, data);
                     return true;
                 }
                 BinaryFormatter formatter = new BinaryFormatter();
                 MemoryStream stream = new MemoryStream(data);
                 value = formatter.Deserialize(stream);
                 return true;
             }
             if (child.Name.Equals("InstanceDescriptor"))
             {
                 value = this.ReadInstanceDescriptor(child, errors);
                 return (value != null);
             }
             errors.Add(string.Format("Unexpected element type {0}", child.Name));
             value = null;
             return false;
         }
         value = null;
         return true;
     }
     catch (Exception ex)
     {
         errors.Add(ex.Message);
         value = null;
         return false;
     }
 }
예제 #5
0
		/// Generic function to read an object value.  Returns true if the read
		/// succeeded.
		private bool ReadValue(XmlNode node, TypeConverter converter, ArrayList errors, out object value)
		{
			try
			{
				foreach (XmlNode child in node.ChildNodes)
				{
					if (child.NodeType == XmlNodeType.Text)
					{
						value = converter.ConvertFromInvariantString(node.InnerText);
						return true;
					}
					else if (child.Name.Equals("Binary"))
					{
						byte[] data = Convert.FromBase64String(child.InnerText);

						// Binary blob.  Now, check to see if the type converter
						// can convert it.  If not, use serialization.
						//
						if (GetConversionSupported(converter, typeof(byte[])))
						{
							value = converter.ConvertFrom(null, CultureInfo.InvariantCulture, data);
							return true;
						}
						else
						{
							BinaryFormatter formatter = new BinaryFormatter();
							MemoryStream stream = new MemoryStream(data);

							value = formatter.Deserialize(stream);
							return true;
						}
					}
					else if (child.Name.Equals("InstanceDescriptor"))
					{
						value = ReadInstanceDescriptor(child, errors);
						return (value != null);
					}
					else
					{
						errors.Add(string.Format("Unexpected element type {0}", child.Name));
						value = null;
						return false;
					}
				}

				// If we get here, it is because there were no nodes.  No nodes and no inner
				// text is how we signify null.
				//
				value = null;
				return true;
			}
			catch (Exception ex)
			{
				errors.Add(ex.Message);
				value = null;
				return false;
			}
		}
예제 #6
0
		object ConvertData (TypeConverter c)
		{
			if (mime_type == ResXResourceWriter.ByteArraySerializedObjectMimeType) {
				if (c.CanConvertFrom (typeof (byte [])))
					return c.ConvertFrom (Convert.FromBase64String (dataString));
			} else if (String.IsNullOrEmpty (mime_type)) {
				if (c.CanConvertFrom (typeof (string)))
					return c.ConvertFromInvariantString (dataString);
			}
			else
				throw new Exception ("shouldnt get here, invalid mime type");

			throw new TypeLoadException ("No converter for this type found");
		}
        /// <summary>
        /// ��ȡ"Value"�ڵ�
        /// �������ԣ��������ĵ��е�����ֵ�������͵�ת����ת��Ϊ��ǰ���Ե�ֵ����
        /// </summary>
        /// <param name="node">���ڵ�</param>
        /// <param name="converter">���͵�ת����</param>
        /// <param name="errors">�����ڼ������Ĵ�������еĻ����ļ���</param>
        /// <param name="value">����</param>
        /// <returns>��ֵ�Ƿ��ܹ���ת������Ҫ������</returns>
        private bool ReadValue(XmlNode node, TypeConverter converter, ArrayList errors, ref object value)
        {
            try
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    //XmlNodeTypeö��ָ���ڵ�����͡�.Text:�ڵ���ı����ݡ�Text �ڵ㲻�ܾ����κ��ӽڵ㡣
                    if (child.NodeType == XmlNodeType.Text)
                    {
                        value = converter.ConvertFromInvariantString(node.InnerText);
                        return true;
                    }
                    else if (child.Name.Equals("Binary"))
                    {
                        byte[] data = Convert.FromBase64String(child.InnerText);

                        if (GetConversionSupported(converter, typeof(byte[])))
                        {
                            value = converter.ConvertFrom(data);
                            return true;
                        }
                        else
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            MemoryStream stream = new MemoryStream(data);

                            value = formatter.Deserialize(stream);
                            return true;
                        }
                    }

                    else
                    {
                        errors.Add(string.Format("��������{0}", child.Name));
                        value = null;
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                value = null;
                return false;
            }
        }
        /// <summary>
        /// ��ȡ"Value"�ڵ�
        /// �������ԣ��������ĵ��е�����ֵ�������͵�ת����ת��Ϊ��ǰ���Ե�ֵ����
        /// </summary>
        /// <param name="node">���ڵ�</param>
        /// <param name="converter">���͵�ת����</param>  
        /// <param name="value">����</param>
        /// <returns>��ֵ�Ƿ��ܹ���ת������Ҫ������</returns>
        private bool ReadValue(XmlNode node, TypeConverter converter, ref object value)
        {
            try
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    //XmlNodeTypeö��ָ���ڵ�����͡�.Text:�ڵ���ı����ݡ�Text �ڵ㲻�ܾ����κ��ӽڵ㡣
                    if (child.NodeType == XmlNodeType.Text)
                    {
                        value = converter.ConvertFromInvariantString(node.InnerText);
                        return true;
                    }
                    else if (child.NodeType == XmlNodeType.Element)
                    {//������Ϣ�ڵ����ô˲��ֶ�ȡ����ӿ���Ϣ20090616
                        List<string> tempIO = new List<string>();
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            tempIO.Add(node.ChildNodes[i].Attributes["name"].Value);
                        }
                        value = String.Join(",", tempIO.ToArray());
                        return true;
                    }
                    else if (child.Name.Equals("Binary"))
                    {
                        byte[] data = Convert.FromBase64String(child.InnerText);

                        if (GetConversionSupported(converter, typeof(byte[])))
                        {
                            value = converter.ConvertFrom(data);
                            return true;
                        }
                        else
                        {
                            BinaryFormatter formatter = new BinaryFormatter();
                            MemoryStream stream = new MemoryStream(data);

                            value = formatter.Deserialize(stream);
                            return true;
                        }
                    }

                    else
                    {
                        value = null;
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                value = null;
                return false;
            }
        }