view Source/Reflection/Extension/ValueCollection.cs @ 9:1e85f66cf767 default tip

update bltoolkit
author nickolay
date Thu, 05 Apr 2018 20:53:26 +0300
parents f990fcb411a9
children
line wrap: on
line source

using System;

namespace BLToolkit.Reflection.Extension
{
	public class ValueCollection : System.Collections.Generic.Dictionary<string,object>
	{
		private object _value;
		public  object  Value
		{
			get { return _value; }
		}

		public new object this[string name]
		{
			get
			{
				object value;
				return TryGetValue(name, out value) ? value : null;
			}
		}

		public void Add(string name, string value)
		{
			if (this != _null)
			{
				var isType = name.EndsWith(TypeExtension.ValueName.TypePostfix);

				if (isType)
				{
					var type      = Type.GetType(value, true);
					var valueName = name.Substring(0, name.Length - TypeExtension.ValueName.TypePostfix.Length);

					Add(name, type);

					object val;

					TryGetValue(valueName, out val);

					if (val != null && val.GetType() != type)
					{
						base[valueName] = val = TypeExtension.ChangeType(val.ToString(), type);

						if (valueName == TypeExtension.ValueName.Value)
							_value = val;
					}
				}
				else
				{
					object val;

					var type = TryGetValue(name + TypeExtension.ValueName.TypePostfix, out val) ? (Type)val : null;

					val  = value;

					if (type != null && type != _value.GetType())
						val = TypeExtension.ChangeType(value, type);

					if (ContainsKey(name))
						base[name] = val;
					else
						Add(name, val);

					if (name == TypeExtension.ValueName.Value)
						_value = val;
				}
			}
		}

		private static readonly Extension.ValueCollection _null = new Extension.ValueCollection();
		public  static          Extension.ValueCollection  Null
		{
			get { return _null;  }
		}
	}
}