static internal string ExportDefaultValue(TypeMapping mapping, object value)
{
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (!(mapping is PrimitiveMapping))
{
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
}
else if (mapping.IsList)
{
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping " + mapping.GetType() + ", should not have Default"));
}
#endif
if (mapping is EnumMapping)
{
EnumMapping em = (EnumMapping)mapping;
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (value.GetType() != typeof(string))
{
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), value.GetType().FullName)));
}
#endif
// check the validity of the value
ConstantMapping[] c = em.Constants;
if (em.IsFlags)
{
string[] names = new string[c.Length];
long[] ids = new long[c.Length];
Hashtable values = new Hashtable();
for (int i = 0; i < c.Length; i++)
{
names[i] = c[i].XmlName;
ids[i] = 1 << i;
values.Add(c[i].Name, ids[i]);
}
long val = XmlCustomFormatter.ToEnum((string)value, values, em.TypeName, false);
return(val != 0 ? XmlCustomFormatter.FromEnum(val, names, ids) : null);
}
else
{
for (int i = 0; i < c.Length; i++)
{
if (c[i].Name == (string)value)
{
return(c[i].XmlName);
}
}
return(null); // unknown value
}
}
PrimitiveMapping pm = (PrimitiveMapping)mapping;
if (!pm.TypeDesc.HasCustomFormatter)
{
#if DEBUG
// use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe
if (pm.TypeDesc.Type == null)
{
throw new InvalidOperationException(Res.GetString(Res.XmlInternalErrorDetails, "Mapping for " + pm.TypeDesc.Name + " missing type property"));
}
#endif
if (pm.TypeDesc.FormatterName == "String")
{
return((string)value);
}
Type formatter = typeof(XmlConvert);
System.Reflection.MethodInfo format = formatter.GetMethod("ToString", new Type[] { pm.TypeDesc.Type });
if (format != null)
{
return((string)format.Invoke(formatter, new Object[] { value }));
}
}
else
{
string defaultValue = XmlCustomFormatter.FromDefaultValue(value, pm.TypeDesc.FormatterName);
if (defaultValue == null)
{
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
}
return(defaultValue);
}
throw new InvalidOperationException(Res.GetString(Res.XmlInvalidDefaultValue, value.ToString(), pm.TypeDesc.Name));
}