private void AddResource()

in tools/simpleresgen/mono/ResXResourceWriter.cs [236:298]


		private void AddResource (string name, object value, string comment)
		{
			if (value is string) {
				AddResource (name, (string) value, comment);
				return;
			}

			if (name == null)
				throw new ArgumentNullException ("name");

			if (value != null && !value.GetType ().IsSerializable)
					throw new InvalidOperationException (String.Format ("The element '{0}' of type '{1}' is not serializable.", name, value.GetType ().Name));

			if (written)
				throw new InvalidOperationException ("The resource is already generated.");

			if (writer == null)
				InitWriter ();

			if (value is byte[]) {
				WriteBytes (name, value.GetType (), (byte []) value, comment);
				return;
			}

			if (value == null) {
				// nulls written as ResXNullRef
				WriteString (name, "", typeof (ResXNullRef), comment);
				return;
			}

			TypeConverter converter = TypeDescriptor.GetConverter (value);
			if (value is ResXFileRef) {
				ResXFileRef fileRef = ProcessFileRefBasePath ((ResXFileRef) value);	
				string str = (string) converter.ConvertToInvariantString (fileRef);
				WriteString (name, str, value.GetType (), comment);
				return;
			}

			if (converter != null && converter.CanConvertTo (typeof (string)) && converter.CanConvertFrom (typeof (string))) {
				string str = (string) converter.ConvertToInvariantString (value);
				WriteString (name, str, value.GetType (), comment);
				return;
			}
			
			if (converter != null && converter.CanConvertTo (typeof (byte[])) && converter.CanConvertFrom (typeof (byte[]))) {
				byte[] b = (byte[]) converter.ConvertTo (value, typeof (byte[]));
				WriteBytes (name, value.GetType (), b, comment);
				return;
			}
			
			MemoryStream ms = new MemoryStream ();
			BinaryFormatter fmt = new BinaryFormatter ();
			try {
				fmt.Serialize (ms, value);
			} catch (Exception e) {
				throw new InvalidOperationException ("Cannot add a " + value.GetType () +
								     "because it cannot be serialized: " +
								     e.Message);
			}

			WriteBytes (name, null, ms.GetBuffer (), 0, (int) ms.Length, comment);
			ms.Close ();
		}