Beispiel #1
0
        public override JsonContract ResolveContract(Type type)
        {
            JsonContract jsonContract;
            Dictionary <StructMultiKey <Type, Type>, JsonContract> structMultiKeys;

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            StructMultiKey <Type, Type> structMultiKey = new StructMultiKey <Type, Type>(base.GetType(), type);
            Dictionary <StructMultiKey <Type, Type>, JsonContract> structMultiKeys1 = CamelCasePropertyNamesContractResolver._contractCache;

            if (structMultiKeys1 == null || !structMultiKeys1.TryGetValue(structMultiKey, out jsonContract))
            {
                jsonContract = this.CreateContract(type);
                lock (CamelCasePropertyNamesContractResolver.TypeContractCacheLock)
                {
                    structMultiKeys1 = CamelCasePropertyNamesContractResolver._contractCache;
                    structMultiKeys  = (structMultiKeys1 != null ? new Dictionary <StructMultiKey <Type, Type>, JsonContract>(structMultiKeys1) : new Dictionary <StructMultiKey <Type, Type>, JsonContract>());
                    structMultiKeys[structMultiKey] = jsonContract;
                    CamelCasePropertyNamesContractResolver._contractCache = structMultiKeys;
                }
            }
            return(jsonContract);
        }
        /// <summary>
        /// Resolves the contract for a given type.
        /// </summary>
        /// <param name="type">The type to resolve a contract for.</param>
        /// <returns>The contract for a given type.</returns>
        public override JsonContract ResolveContract(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            // for backwards compadibility the CamelCasePropertyNamesContractResolver shares contracts between instances
            StructMultiKey <Type, Type> key = new StructMultiKey <Type, Type>(GetType(), type);
            Dictionary <StructMultiKey <Type, Type>, JsonContract>?cache = _contractCache;

            if (cache == null || !cache.TryGetValue(key, out JsonContract contract))
            {
                contract = CreateContract(type);

                // avoid the possibility of modifying the cache dictionary while another thread is accessing it
                lock (TypeContractCacheLock)
                {
                    cache = _contractCache;
                    Dictionary <StructMultiKey <Type, Type>, JsonContract> updatedCache = (cache != null)
                        ? new Dictionary <StructMultiKey <Type, Type>, JsonContract>(cache)
                        : new Dictionary <StructMultiKey <Type, Type>, JsonContract>();
                    updatedCache[key] = contract;

                    _contractCache = updatedCache;
                }
            }

            return(contract);
        }
Beispiel #3
0
 // Token: 0x06000EDF RID: 3807 RVA: 0x00055D38 File Offset: 0x00053F38
 private Type GetTypeByName([Nullable(new byte[]
 {
     0,
     2,
     1
 })] StructMultiKey <string, string> typeNameKey)
 {
     return(this._typeCache.Get(typeNameKey));
 }
        private Type?GetGenericTypeFromTypeName(string typeName, Assembly assembly)
        {
            Type?type             = null;
            int  openBracketIndex = typeName.IndexOf('[');

            if (openBracketIndex >= 0)
            {
                string genericTypeDefName = typeName.Substring(0, openBracketIndex);
                Type   genericTypeDef     = assembly.GetType(genericTypeDefName);
                if (genericTypeDef != null)
                {
                    List <Type> genericTypeArguments = new List <Type>();
                    int         scope             = 0;
                    int         typeArgStartIndex = 0;
                    int         endIndex          = typeName.Length - 1;
                    for (int i = openBracketIndex + 1; i < endIndex; ++i)
                    {
                        char current = typeName[i];
                        switch (current)
                        {
                        case '[':
                            if (scope == 0)
                            {
                                typeArgStartIndex = i + 1;
                            }
                            ++scope;
                            break;

                        case ']':
                            --scope;
                            if (scope == 0)
                            {
                                string typeArgAssemblyQualifiedName = typeName.Substring(
                                    typeArgStartIndex,
                                    i - typeArgStartIndex
                                    );

                                StructMultiKey <string?, string> typeNameKey =
                                    ReflectionUtils.SplitFullyQualifiedTypeName(
                                        typeArgAssemblyQualifiedName
                                        );
                                genericTypeArguments.Add(GetTypeByName(typeNameKey));
                            }
                            break;
                        }
                    }

                    type = genericTypeDef.MakeGenericType(genericTypeArguments.ToArray());
                }
            }

            return(type);
        }
Beispiel #5
0
        // Token: 0x06000EDD RID: 3805 RVA: 0x00055B08 File Offset: 0x00053D08
        private Type GetTypeFromTypeNameKey([Nullable(new byte[]
        {
            0,
            2,
            1
        })] StructMultiKey <string, string> typeNameKey)
        {
            string value  = typeNameKey.Value1;
            string value2 = typeNameKey.Value2;

            if (value == null)
            {
                return(Type.GetType(value2));
            }
            Assembly assembly = Assembly.LoadWithPartialName(value);

            if (assembly == null)
            {
                foreach (Assembly assembly2 in AppDomain.CurrentDomain.GetAssemblies())
                {
                    if (assembly2.FullName == value || assembly2.GetName().Name == value)
                    {
                        assembly = assembly2;
                        break;
                    }
                }
            }
            if (assembly == null)
            {
                throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, value));
            }
            Type type = assembly.GetType(value2);

            if (type == null)
            {
                if (value2.IndexOf('`') >= 0)
                {
                    try
                    {
                        type = this.GetGenericTypeFromTypeName(value2, assembly);
                    }
                    catch (Exception innerException)
                    {
                        throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName), innerException);
                    }
                }
                if (type == null)
                {
                    throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName));
                }
            }
            return(type);
        }
Beispiel #6
0
        private Type GetGenericTypeFromTypeName(string typeName, Assembly assembly)
        {
            Type result = null;

            int num = typeName.IndexOf('[');

            if (num >= 0)
            {
                string name = typeName.Substring(0, num);
                Type   type = assembly.GetType(name);
                if (type != null)
                {
                    List <Type> list = new List <Type>();
                    int         num2 = 0;
                    int         num3 = 0;
                    int         num4 = typeName.Length - 1;
                    for (int i = num + 1; i < num4; i++)
                    {
                        char c = typeName[i];
                        if (c != '[')
                        {
                            if (c == ']')
                            {
                                num2--;
                                if (num2 == 0)
                                {
                                    StructMultiKey <string, string> typeNameKey = ReflectionUtils.SplitFullyQualifiedTypeName(typeName.Substring(num3, i - num3));
                                    list.Add(this.GetTypeByName(typeNameKey));
                                }
                            }
                        }
                        else
                        {
                            if (num2 == 0)
                            {
                                num3 = i + 1;
                            }
                            num2++;
                        }
                    }
                    result = type.MakeGenericType(list.ToArray());
                }
            }
            return(result);
        }
        private Type GetGenericTypeFromTypeName(string typeName, Assembly assembly)
        {
            Type type = null;
            int  num  = typeName.IndexOf('[');

            if (num >= 0)
            {
                Type type1 = assembly.GetType(typeName.Substring(0, num));
                if (type1 != null)
                {
                    List <Type> types  = new List <Type>();
                    int         num1   = 0;
                    int         num2   = 0;
                    int         length = typeName.Length - 1;
                    for (int i = num + 1; i < length; i++)
                    {
                        char chr = typeName[i];
                        if (chr == '[')
                        {
                            if (num1 == 0)
                            {
                                num2 = i + 1;
                            }
                            num1++;
                        }
                        else if (chr == ']')
                        {
                            num1--;
                            if (num1 == 0)
                            {
                                StructMultiKey <string, string> structMultiKey = ReflectionUtils.SplitFullyQualifiedTypeName(typeName.Substring(num2, i - num2));
                                types.Add(this.GetTypeByName(structMultiKey));
                            }
                        }
                    }
                    type = type1.MakeGenericType(types.ToArray());
                }
            }
            return(type);
        }
        // Token: 0x06001093 RID: 4243 RVA: 0x0005E440 File Offset: 0x0005C640
        public override JsonContract ResolveContract(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            StructMultiKey <Type, Type> key = new StructMultiKey <Type, Type>(base.GetType(), type);
            Dictionary <StructMultiKey <Type, Type>, JsonContract> contractCache = CamelCasePropertyNamesContractResolver._contractCache;
            JsonContract jsonContract;

            if (contractCache == null || !contractCache.TryGetValue(key, out jsonContract))
            {
                jsonContract = this.CreateContract(type);
                object typeContractCacheLock = CamelCasePropertyNamesContractResolver.TypeContractCacheLock;
                lock (typeContractCacheLock)
                {
                    contractCache = CamelCasePropertyNamesContractResolver._contractCache;
                    Dictionary <StructMultiKey <Type, Type>, JsonContract> dictionary = (contractCache != null) ? new Dictionary <StructMultiKey <Type, Type>, JsonContract>(contractCache) : new Dictionary <StructMultiKey <Type, Type>, JsonContract>();
                    dictionary[key] = jsonContract;
                    CamelCasePropertyNamesContractResolver._contractCache = dictionary;
                }
            }
            return(jsonContract);
        }
        private Type GetTypeFromTypeNameKey(StructMultiKey <string, string> typeNameKey)
        {
            string assemblyName = typeNameKey.Value1;
            string typeName     = typeNameKey.Value2;

            if (assemblyName != null)
            {
                Assembly assembly;

#if !(DOTNET || PORTABLE40 || PORTABLE)
                // look, I don't like using obsolete methods as much as you do but this is the only way
                // Assembly.Load won't check the GAC for a partial name
#pragma warning disable 618,612
                assembly = Assembly.LoadWithPartialName(assemblyName);
#pragma warning restore 618,612
#elif DOTNET || PORTABLE
                assembly = Assembly.Load(new AssemblyName(assemblyName));
#else
                assembly = Assembly.Load(assemblyName);
#endif

#if HAVE_APP_DOMAIN
                if (assembly == null)
                {
                    // will find assemblies loaded with Assembly.LoadFile outside of the main directory
                    Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
                    foreach (Assembly a in loadedAssemblies)
                    {
                        // check for both full name or partial name match
                        if (a.FullName == assemblyName || a.GetName().Name == assemblyName)
                        {
                            assembly = a;
                            break;
                        }
                    }
                }
#endif

                if (assembly == null)
                {
                    throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, assemblyName));
                }

                Type type = assembly.GetType(typeName);
                if (type == null)
                {
                    // if generic type, try manually parsing the type arguments for the case of dynamically loaded assemblies
                    // example generic typeName format: System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
                    if (typeName.IndexOf('`') >= 0)
                    {
                        try
                        {
                            type = GetGenericTypeFromTypeName(typeName, assembly);
                        }
                        catch (Exception ex)
                        {
                            throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName), ex);
                        }
                    }

                    if (type == null)
                    {
                        throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, typeName, assembly.FullName));
                    }
                }

                return(type);
            }
            else
            {
                return(Type.GetType(typeName));
            }
        }
 private Type GetTypeByName(StructMultiKey <string, string> typeNameKey)
 {
     return(_typeCache.Get(typeNameKey));
 }
        private Type GetTypeFromTypeNameKey(StructMultiKey <string, string> typeNameKey)
        {
            Type      type;
            Exception exception;
            string    value1 = typeNameKey.Value1;
            string    value2 = typeNameKey.Value2;

            if (value1 == null)
            {
                return(Type.GetType(value2));
            }
            Assembly assembly = Assembly.LoadWithPartialName(value1);

            if (assembly == null)
            {
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
                int        num        = 0;
                while (num < (int)assemblies.Length)
                {
                    Assembly assembly1 = assemblies[num];
                    if (assembly1.FullName == value1 || assembly1.GetName().Name == value1)
                    {
                        assembly = assembly1;
                        if (assembly == null)
                        {
                            throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, value1));
                        }
                        type = assembly.GetType(value2);
                        if (type == null)
                        {
                            if (value2.IndexOf('\u0060') >= 0)
                            {
                                try
                                {
                                    type = this.GetGenericTypeFromTypeName(value2, assembly);
                                }
                                catch (Exception exception1)
                                {
                                    exception = exception1;
                                    throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName), exception);
                                }
                            }
                            if (type == null)
                            {
                                throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName));
                            }
                        }
                        return(type);
                    }
                    else
                    {
                        num++;
                    }
                }
            }
            if (assembly == null)
            {
                throw new JsonSerializationException("Could not load assembly '{0}'.".FormatWith(CultureInfo.InvariantCulture, value1));
            }
            type = assembly.GetType(value2);
            if (type == null)
            {
                if (value2.IndexOf('\u0060') >= 0)
                {
                    try
                    {
                        type = this.GetGenericTypeFromTypeName(value2, assembly);
                    }
                    catch (Exception exception1)
                    {
                        exception = exception1;
                        throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName), exception);
                    }
                }
                if (type == null)
                {
                    throw new JsonSerializationException("Could not find type '{0}' in assembly '{1}'.".FormatWith(CultureInfo.InvariantCulture, value2, assembly.FullName));
                }
            }
            return(type);
        }