예제 #1
0
        private static bool Match(Type baseType, Type type)
        {
            var r = TypeTools.IsAssignableFrom(baseType, type);

            if (r)
            {
                return(true);
            }

            if (!baseType.IsGenericType())
            {
                return(false);
            }

            if (baseType.IsInterface())
            {
                return(TypeTools.GetInterfaces(type).Select(GetUnderlinedGenericType).Any(t => baseType == t));
            }

            if (type.IsInterface())
            {
                return(false);
            }

            do
            {
                type = GetUnderlinedGenericType(type);
                if (baseType == type)
                {
                    return(true);
                }
                type = type.GetBaseType();
            } while (type != null);
            return(false);
        }
예제 #2
0
        public static IEnumerable <FieldInfo> GetSerializableFields(Type type, bool includeBases)
        {
            while (type?.GetBaseType() != null && type.IsSerializable())
            {
                var fields = TypeTools.GetRuntimeFields(type);
                foreach (var f in fields.Where(f => f.FieldType.IsSerializable() && !f.IsNotSerialized()))
                {
                    yield return(f);
                }

                if (!includeBases)
                {
                    break;
                }
                type = type.GetBaseType();
            }
        }
예제 #3
0
        private static void UpdateSerializationSystems()
        {
            if (!_isDirty)
            {
                return;
            }

            _assemblyLoader.OnAssemblyLoaded -= SetDirty;

            Dictionary <Type, List <Pair> > candidates = new Dictionary <Type, List <Pair> >();

            var searchSet = new Queue <Assembly>(_assemblyLoader.GetAssemblies());
            var foundSet  = new HashSet <Assembly>();

            Action <Assembly> onAssemblyLoaded = a =>
            {
                if (foundSet.Contains(a))
                {
                    return;
                }

                searchSet.Enqueue(a);
            };

            _assemblyLoader.OnAssemblyLoaded += onAssemblyLoaded;

            while (searchSet.Count > 0)
            {
                var ass = searchSet.Dequeue();
                foundSet.Add(ass);

                foreach (var type in TypeTools.GetAllTypes(ass).Where(t => !(t.IsAbstract() || t.IsInterface())))
                {
                    var key = _validTypes.FirstOrDefault(i => TypeTools.IsAssignableFrom(i, type));
                    if (key == null)
                    {
                        continue;
                    }

                    var att = type.GetTypeAttributes <DefaultSerializationSystemAttribute>(false).FirstOrDefault();
                    if (att == null)
                    {
                        continue;
                    }

                    List <Pair> set;
                    if (!candidates.TryGetValue(key, out set))
                    {
                        set             = new List <Pair>();
                        candidates[key] = set;
                    }

                    set.Add(Tuples.Create(att, type));
                }
            }

            _assemblyLoader.OnAssemblyLoaded -= onAssemblyLoaded;

            RecalcTypeTrees(_surrogateSelector, candidates);
            RecalcTypeTrees(_formatterSelector, candidates);

            _assemblyLoader.OnAssemblyLoaded += SetDirty;
            _isDirty = false;
        }