object DoGetCorrectlyTypedValue (XamlType xt, object value)
{
if (value == null) {
if (xt.IsContentValue (service_provider)) // it is for collection/dictionary key and item
return null;
else
return xt.IsNullable ? null : xt.Invoker.CreateInstance (new object [0]);
}
if (xt == null)
return value;
// Not sure if this is really required though...
var vt = sctx.GetXamlType (value.GetType ());
if (vt.CanAssignTo (xt))
return value;
// FIXME: this could be generalized by some means, but I cannot find any.
if (xt.UnderlyingType == typeof (XamlType) && value is string)
value = ResolveTypeFromName ((string) value);
// FIXME: this could be generalized by some means, but I cannot find any.
if (xt.UnderlyingType == typeof (Type))
value = new TypeExtension ((string) value).ProvideValue (service_provider);
if (xt == XamlLanguage.Type && value is string)
value = new TypeExtension ((string) value);
if (IsAllowedType (xt, value))
return value;
if (xt.TypeConverter != null && value != null) {
var tc = xt.TypeConverter.ConverterInstance;
if (tc != null && tc.CanConvertFrom (value.GetType ()))
value = tc.ConvertFrom (value);
return value;
}
throw new XamlObjectWriterException (String.Format ("Value '{1}' (of type {2}) is not of or convertible to type {0}", xt, value, value != null ? (object) value.GetType () : "(null)"));
}