csharp/akaskela/WorkflowElements/akaskela-WorkflowElements-3f75e9a/Kaskela.WorkflowElements.Shared/Newtonsoft/Serialization/DefaultSerializationBinder.cs

DefaultSerializationBinder.cs
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and astociated docameentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Runtime.Serialization;
using System.Reflection;
using System.Globalization;
using Newtonsoft.Json.Utilities;

namespace Newtonsoft.Json.Serialization
{
    /// 
    /// The default serialization binder used when resolving and loading clastes from type names.
    /// 
    public clast DefaultSerializationBinder : SerializationBinder
    {
        internal static readonly DefaultSerializationBinder Instance = new DefaultSerializationBinder();

        private readonly ThreadSafeStore _typeCache = new ThreadSafeStore(GetTypeFromTypeNameKey);

        private static Type GetTypeFromTypeNameKey(TypeNameKey typeNameKey)
        {
            string astemblyName = typeNameKey.astemblyName;
            string typeName = typeNameKey.TypeName;

            if (astemblyName != null)
            {
                astembly astembly;

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

#if !(PORTABLE40 || PORTABLE || DOTNET)
                if (astembly == null)
                {
                    // will find astemblies loaded with astembly.LoadFile outside of the main directory
                    astembly[] loadedastemblies = AppDomain.CurrentDomain.Getastemblies();
                    foreach (astembly a in loadedastemblies)
                    {
                        if (a.FullName == astemblyName)
                        {
                            astembly = a;
                            break;
                        }
                    }
                }
#endif

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

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

                return type;
            }
            else
            {
                return Type.GetType(typeName);
            }
        }

        internal struct TypeNameKey : IEquatable
        {
            internal readonly string astemblyName;
            internal readonly string TypeName;

            public TypeNameKey(string astemblyName, string typeName)
            {
                astemblyName = astemblyName;
                TypeName = typeName;
            }

            public override int GetHashCode()
            {
                return ((astemblyName != null) ? astemblyName.GetHashCode() : 0)
                       ^ ((TypeName != null) ? TypeName.GetHashCode() : 0);
            }

            public override bool Equals(object obj)
            {
                if (!(obj is TypeNameKey))
                {
                    return false;
                }

                return Equals((TypeNameKey)obj);
            }

            public bool Equals(TypeNameKey other)
            {
                return (astemblyName == other.astemblyName && TypeName == other.TypeName);
            }
        }

        /// 
        /// When overridden in a derived clast, controls the binding of a serialized object to a type.
        /// 
        /// Specifies the  name of the serialized object.
        /// Specifies the  name of the serialized object.
        /// 
        /// The type of the object the formatter creates a new instance of.
        /// 
        public override Type BindToType(string astemblyName, string typeName)
        {
            return _typeCache.Get(new TypeNameKey(astemblyName, typeName));
        }

#if !(NET35 || NET20)
        /// 
        /// When overridden in a derived clast, controls the binding of a serialized object to a type.
        /// 
        /// The type of the object the formatter creates a new instance of.
        /// Specifies the  name of the serialized object. 
        /// Specifies the  name of the serialized object. 
        public override void BindToName(Type serializedType, out string astemblyName, out string typeName)
        {
#if (DOTNET || PORTABLE)
            astemblyName = serializedType.GetTypeInfo().astembly.FullName;
            typeName = serializedType.FullName;
#else
            astemblyName = serializedType.astembly.FullName;
            typeName = serializedType.FullName;
#endif
        }
#endif
    }
}