本文整理汇总了C#中JavaScriptSerializer.ConverterExistsForType方法的典型用法代码示例。如果您正苦于以下问题:C# JavaScriptSerializer.ConverterExistsForType方法的具体用法?C# JavaScriptSerializer.ConverterExistsForType怎么用?C# JavaScriptSerializer.ConverterExistsForType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JavaScriptSerializer
的用法示例。
在下文中一共展示了JavaScriptSerializer.ConverterExistsForType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: IsClientInstantiatableType
internal static bool IsClientInstantiatableType(Type t, JavaScriptSerializer serializer)
{
if (((t == null) || t.IsAbstract) || (t.IsInterface || t.IsArray))
{
return false;
}
if (t == typeof(object))
{
return false;
}
JavaScriptConverter converter = null;
if (!serializer.ConverterExistsForType(t, out converter))
{
if (t.IsValueType)
{
return true;
}
if (t.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null) == null)
{
return false;
}
}
return true;
}
示例2: ConvertDictionaryToObject
private static bool ConvertDictionaryToObject(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject, string[] extraTimeFormats)
{
object obj2;
Type t = type;
string id = null;
object o = dictionary;
if (dictionary.TryGetValue("__type", out obj2))
{
if (!ConvertObjectToTypeMain(obj2, typeof(string), serializer, throwOnError, out obj2, extraTimeFormats))
{
convertedObject = false;
return false;
}
id = (string)obj2;
if (id != null)
{
if (serializer.TypeResolver != null)
{
t = serializer.TypeResolver.ResolveType(id);
if (t == null)
{
if (throwOnError)
{
throw new InvalidOperationException();
}
convertedObject = null;
return false;
}
}
dictionary.Remove("__type");
}
}
JavaScriptConverter converter = null;
if ((t != null) && serializer.ConverterExistsForType(t, out converter))
{
try
{
convertedObject = converter.Deserialize(dictionary, t, serializer, extraTimeFormats);
return true;
}
catch
{
if (throwOnError)
{
throw;
}
convertedObject = null;
return false;
}
}
if ((id != null) || IsClientInstantiatableType(t, serializer))
{
o = Activator.CreateInstance(t);
}
List<string> list = new List<string>(dictionary.Keys);
if (IsGenericDictionary(type))
{
Type type3 = type.GetGenericArguments()[0];
if ((type3 != typeof(string)) && (type3 != typeof(object)))
{
if (throwOnError)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, new object[] { type.FullName }));
}
convertedObject = null;
return false;
}
Type type4 = type.GetGenericArguments()[1];
IDictionary dictionary2 = null;
if (IsClientInstantiatableType(type, serializer))
{
dictionary2 = (IDictionary)Activator.CreateInstance(type);
}
else
{
dictionary2 = (IDictionary)Activator.CreateInstance(_dictionaryGenericType.MakeGenericType(new Type[] { type3, type4 }));
}
if (dictionary2 != null)
{
foreach (string str2 in list)
{
object obj4;
if (!ConvertObjectToTypeMain(dictionary[str2], type4, serializer, throwOnError, out obj4, extraTimeFormats))
{
convertedObject = null;
return false;
}
dictionary2[str2] = obj4;
}
convertedObject = dictionary2;
return true;
}
}
if ((type != null) && !type.IsAssignableFrom(o.GetType()))
{
if (!throwOnError)
{
convertedObject = null;
return false;
}
//.........这里部分代码省略.........
示例3: ConvertDictionaryToObject
// Method that converts an IDictionary<string, object> to an object of the right type
private static bool ConvertDictionaryToObject(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer, bool throwOnError, out object convertedObject) {
// The target type to instantiate.
Type targetType = type;
object s;
string serverTypeName = null;
object o = dictionary;
// Check if __serverType exists in the dictionary, use it as the type.
if (dictionary.TryGetValue(JavaScriptSerializer.ServerTypeFieldName, out s)) {
// Convert the __serverType value to a string.
if (!ConvertObjectToTypeMain(s, typeof(String), serializer, throwOnError, out s)) {
convertedObject = false;
return false;
}
serverTypeName = (string)s;
if (serverTypeName != null) {
// If we don't have the JavaScriptTypeResolver, we can't use it
if (serializer.TypeResolver != null) {
// Get the actual type from the resolver.
targetType = serializer.TypeResolver.ResolveType(serverTypeName);
// In theory, we should always find the type. If not, it may be some kind of attack.
if (targetType == null) {
if (throwOnError) {
throw new InvalidOperationException();
}
convertedObject = null;
return false;
}
}
// Remove the serverType from the dictionary, even if the resolver was null
dictionary.Remove(JavaScriptSerializer.ServerTypeFieldName);
}
}
JavaScriptConverter converter = null;
if (targetType != null && serializer.ConverterExistsForType(targetType, out converter)) {
try {
convertedObject = converter.Deserialize(dictionary, targetType, serializer);
return true;
}
catch {
if (throwOnError) {
throw;
}
convertedObject = null;
return false;
}
}
// Instantiate the type if it's coming from the __serverType argument.
if (serverTypeName != null || IsClientInstantiatableType(targetType, serializer)) {
// First instantiate the object based on the type.
o = Activator.CreateInstance(targetType);
}
#if INDIGO
StructuralContract contract = null;
if (suggestedType != null &&
suggestedType.GetCustomAttributes(typeof(DataContractAttribute), false).Length > 0)
contract = StructuralContract.Create(suggestedType);
#endif
// Use a different collection to avoid modifying the original during keys enumeration.
List<String> memberNames = new List<String>(dictionary.Keys);
// Try to handle the IDictionary<K, V> case
if (IsGenericDictionary(type)) {
Type keyType = type.GetGenericArguments()[0];
if (keyType != typeof(string) && keyType != typeof(object)) {
if (throwOnError) {
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, type.FullName));
}
convertedObject = null;
return false;
}
Type valueType = type.GetGenericArguments()[1];
IDictionary dict = null;
if (IsClientInstantiatableType(type, serializer)) {
dict = (IDictionary)Activator.CreateInstance(type);
}
else {
// Get the strongly typed Dictionary<K, V>
Type t = _dictionaryGenericType.MakeGenericType(keyType, valueType);
dict = (IDictionary)Activator.CreateInstance(t);
}
if (dict != null) {
//.........这里部分代码省略.........
示例4: IsClientInstantiatableType
// Is this a type for which we want to instantiate based on the client stub
internal static bool IsClientInstantiatableType(Type t, JavaScriptSerializer serializer) {
// Abstract classes and interfaces can't be instantiated
//
if (t == null || t.IsAbstract || t.IsInterface || t.IsArray)
return false;
// Even though 'object' is instantiatable, it is never useful to do this
if (t == typeof(object))
return false;
// Return true if a converter is registered for the given type, so the converter
// can generate code on the client to instantiate it.
JavaScriptConverter converter = null;
if (serializer.ConverterExistsForType(t, out converter)) {
return true;
}
// Value types are okay (i.e. structs);
if (t.IsValueType) {
return true;
}
// Ignore types that don't have a public default ctor
ConstructorInfo constructorInfo = t.GetConstructor(BindingFlags.Public | BindingFlags.Instance,
null, s_emptyTypeArray, null);
if (constructorInfo == null)
return false;
return true;
}
示例5: ConvertDictionaryToObject
private static object ConvertDictionaryToObject(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
object obj2;
Type t = type;
string id = null;
object o = dictionary;
if (dictionary.TryGetValue("__type", out obj2))
{
id = ConvertObjectToType(obj2, typeof(string), serializer) as string;
if (id != null)
{
if (serializer.TypeResolver != null)
{
t = serializer.TypeResolver.ResolveType(id);
if (t == null)
{
throw new InvalidOperationException();
}
}
dictionary.Remove("__type");
}
}
JavaScriptConverter converter = null;
if ((t != null) && serializer.ConverterExistsForType(t, out converter))
{
return converter.Deserialize(dictionary, t, serializer);
}
if ((id != null) || ((t != null) && IsClientInstantiatableType(t, serializer)))
{
o = Activator.CreateInstance(t);
}
List<string> list = new List<string>(dictionary.Keys);
if ((((type != null) && type.IsGenericType) && (typeof(IDictionary).IsAssignableFrom(type) || (type.GetGenericTypeDefinition() == _idictionaryGenericType))) && (type.GetGenericArguments().Length == 2))
{
Type type3 = type.GetGenericArguments()[0];
if ((type3 != typeof(string)) && (type3 != typeof(object)))
{
throw new InvalidOperationException();//string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DictionaryTypeNotSupported, new object[] { type.FullName }));
}
Type type4 = type.GetGenericArguments()[1];
IDictionary dictionary2 = null;
if (IsClientInstantiatableType(type, serializer))
{
dictionary2 = (IDictionary)Activator.CreateInstance(type);
}
else
{
dictionary2 = (IDictionary)Activator.CreateInstance(_dictionaryGenericType.MakeGenericType(new Type[] { type3, type4 }));
}
if (dictionary2 != null)
{
foreach (string str2 in list)
{
dictionary2[str2] = ConvertObjectToType(dictionary[str2], type4, serializer);
}
return dictionary2;
}
}
if ((type != null) && !type.IsAssignableFrom(o.GetType()))
{
if (type.GetConstructor(BindingFlags.Public | BindingFlags.Instance, null, s_emptyTypeArray, null) == null)
{
throw new MissingMethodException();//string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_NoConstructor, new object[] { type.FullName }));
}
throw new InvalidOperationException();//string.Format(CultureInfo.InvariantCulture, AtlasWeb.JSON_DeserializerTypeMismatch, new object[] { type.FullName }));
}
foreach (string str3 in list)
{
object propertyValue = dictionary[str3];
AssignToPropertyOrField(propertyValue, o, str3, serializer);
}
return o;
}