本文整理汇总了C#中JavaScriptSerializer.TryGetConverter方法的典型用法代码示例。如果您正苦于以下问题:C# JavaScriptSerializer.TryGetConverter方法的具体用法?C# JavaScriptSerializer.TryGetConverter怎么用?C# JavaScriptSerializer.TryGetConverter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JavaScriptSerializer
的用法示例。
在下文中一共展示了JavaScriptSerializer.TryGetConverter方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsClientInstantiatableType
internal static bool IsClientInstantiatableType(Type type,
JavaScriptSerializer serializer)
{
if (type == null)
return false;
if (type.IsAbstract)
return false;
if (type.IsInterface)
return false;
if (type.IsArray)
return false;
if (type == typeof(object))
return false;
JavaScriptConverter converter = null;
if (serializer.TryGetConverter(type, out converter))
return true;
if (type.IsValueType)
return true;
if (type.GetConstructor(BindingFlags.Instance | BindingFlags.Public,
null, _emptyTypeArray, null) != null)
return true;
return false;
}
示例2: ConvertDictionary
private static bool ConvertDictionary(IDictionary<string, object> dictionary,
Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject)
{
Type t = type;
string typeId = null;
object customObject = dictionary;
object typeKey;
if (dictionary.TryGetValue(JavaScriptSerializer.ServerTypeFieldName, out typeKey))
{
if (!TryConvertObjectToType(typeKey, typeof(string), serializer, throwOnError, out typeKey))
{
convertedObject = null;
return false;
}
typeId = (string)typeKey;
if (typeId != null)
{
if (serializer.TypeResolver != null)
{
t = serializer.TypeResolver.ResolveType(typeId);
if (t == null)
{
if (throwOnError)
throw Error.CouldNotResolveType(typeId);
convertedObject = null;
return false;
}
}
dictionary.Remove(JavaScriptSerializer.ServerTypeFieldName);
}
}
JavaScriptConverter converter = null;
if (t != null && serializer.TryGetConverter(t, out converter))
{
try
{
convertedObject = converter.Deserialize(dictionary, t, serializer);
return true;
}
catch
{
if (throwOnError)
throw;
convertedObject = null;
return false;
}
}
if (typeId != null || IsClientInstantiatableType(t, serializer))
customObject = Activator.CreateInstance(t);
List<string> list = new List<string>(dictionary.Keys);
if (IsGenericDictionary(type))
{
Type keyType = type.GetGenericArguments()[0];
if (!JavaScriptSerializer.IsSupportedKeyType(keyType))
{
if (throwOnError)
throw Error.SuppliedDictionaryTypeIsNotSupported();
convertedObject = null;
return false;
}
Type valueType = type.GetGenericArguments()[1];
IDictionary dict = null;
if (IsClientInstantiatableType(type, serializer))
dict = (IDictionary)Activator.CreateInstance(type);
else
dict = (IDictionary)Activator.CreateInstance(
_dictionaryGenericType.MakeGenericType(keyType, valueType));
if (dict != null)
{
foreach (string key in list)
{
object convertedKey = Converter.ChangeType(keyType,
(object)key, CultureInfo.InvariantCulture);
object value;
if (!TryConvertObjectToType(dictionary[key],
valueType, serializer, throwOnError, out value))
{
convertedObject = null;
return false;
}
dict[convertedKey] = value;
}
convertedObject = dict;
return true;
}
}
//.........这里部分代码省略.........