示例#1
0
 protected internal static void ThrowUnexpectedSubtype(Type expected, Type actual)
 {
     if (expected != TypeModel.ResolveProxies(actual))
     {
         throw new InvalidOperationException("Unexpected sub-type: " + actual.FullName);
     }
 }
示例#2
0
        protected internal int GetKey(ref Type type)
        {
            if (type == null)
            {
                return(-1);
            }
            int key = this.GetKeyImpl(type);

            if (key < 0)
            {
                Type normalized = TypeModel.ResolveProxies(type);
                if (normalized != null)
                {
                    type = normalized;
                    key  = this.GetKeyImpl(type);
                }
            }
            return(key);
        }
示例#3
0
        internal MetaType FindWithoutAdd(Type type)
        {
            BasicList.NodeEnumerator enumerator = this.types.GetEnumerator();
            while (enumerator.MoveNext())
            {
                MetaType metaType = (MetaType)enumerator.Current;
                if (metaType.Type == type)
                {
                    if (metaType.Pending)
                    {
                        this.WaitOnLock(metaType);
                    }
                    return(metaType);
                }
            }
            Type underlyingType = TypeModel.ResolveProxies(type);

            if (!(underlyingType == null))
            {
                return(this.FindWithoutAdd(underlyingType));
            }
            return(null);
        }
示例#4
0
        internal int FindOrAddAuto(Type type, bool demand, bool addWithContractOnly, bool addEvenIfAutoDisabled)
        {
            int key = this.types.IndexOf(RuntimeTypeModel.MetaTypeFinder, type);

            if (key >= 0)
            {
                MetaType metaType = (MetaType)this.types[key];
                if (metaType.Pending)
                {
                    this.WaitOnLock(metaType);
                }
                return(key);
            }
            bool shouldAdd = this.AutoAddMissingTypes || addEvenIfAutoDisabled;

            if (Helpers.IsEnum(type) || this.TryGetBasicTypeSerializer(type) == null)
            {
                Type underlyingType = TypeModel.ResolveProxies(type);
                if (underlyingType != null)
                {
                    key  = this.types.IndexOf(RuntimeTypeModel.MetaTypeFinder, underlyingType);
                    type = underlyingType;
                }
                if (key < 0)
                {
                    int opaqueToken = 0;
                    try
                    {
                        this.TakeLock(ref opaqueToken);
                        MetaType metaType;
                        if ((metaType = this.RecogniseCommonTypes(type)) == null)
                        {
                            MetaType.AttributeFamily family = MetaType.GetContractFamily(this, type, null);
                            if (family == MetaType.AttributeFamily.AutoTuple)
                            {
                                addEvenIfAutoDisabled = (shouldAdd = true);
                            }
                            if (!shouldAdd || (!Helpers.IsEnum(type) && addWithContractOnly && family == MetaType.AttributeFamily.None))
                            {
                                if (demand)
                                {
                                    TypeModel.ThrowUnexpectedType(type);
                                }
                                return(key);
                            }
                            metaType = this.Create(type);
                        }
                        metaType.Pending = true;
                        bool weAdded = false;
                        int  winner  = this.types.IndexOf(RuntimeTypeModel.MetaTypeFinder, type);
                        if (winner < 0)
                        {
                            this.ThrowIfFrozen();
                            key     = this.types.Add(metaType);
                            weAdded = true;
                        }
                        else
                        {
                            key = winner;
                        }
                        if (weAdded)
                        {
                            metaType.ApplyDefaultBehaviour();
                            metaType.Pending = false;
                        }
                    }
                    finally
                    {
                        this.ReleaseLock(opaqueToken);
                    }
                    return(key);
                }
                return(key);
            }
            if (shouldAdd && !addWithContractOnly)
            {
                throw MetaType.InbuiltType(type);
            }
            return(-1);
        }