コード例 #1
0
        public StringEnumMap(int id, string stringRepresentation, List <string> otherPossibleRepresentation = null)
        {
            CachedType = GetType();
            _id        = id;
            if (_fromId.ContainsKey(_id))
            {
                throw new DuplicateNameException(
                          $"Cannot have two options with same id ({_id}). Used by: {_fromId[id]}. Tried to set to: {stringRepresentation}.");
            }

            _fromId[_id] = this;
            // Store string repr to dictionary for access later.
            _stringRepresentation = stringRepresentation;
            if (!string.IsNullOrEmpty(_stringRepresentation))
            {
                if (_fromString.ContainsKey(_stringRepresentation))
                {
                    throw new DuplicateNameException($"Key: {_stringRepresentation} is not unique. Also belongs to: {_fromString[(_stringRepresentation)]}.");
                }

                // Store the enum to string repr.
                _toString[this] = _stringRepresentation;
                _fromString[_stringRepresentation] = this;
            }
            else
            {
                throw new TypeInitializationException(CachedType.ToString(),
                                                      new Exception("A string representation is required for elements."));
            }

            if (otherPossibleRepresentation != null)
            {
                // Store other possible strings reprs
                _otherPossibleRepresentation = otherPossibleRepresentation;
                foreach (var str in _otherPossibleRepresentation)
                {
                    if (_fromString.ContainsKey(str))
                    {
                        throw new DuplicateNameException($"Key: {str} is not unique. Also belongs to: {_fromString[str]}.");
                    }

                    _fromString[str] = this;
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Get cached type data.
        /// </summary>

        static public CachedType GetCache(this Type type)
        {
            if (mTypeDict == null)
            {
                CacheTypes();
            }
            CachedType ent = null;

            if (!mTypeDict.TryGetValue(type, out ent))
            {
                ent      = new CachedType();
                ent.type = type;
                ent.name = ent.ToString();
                mCachedTypes.Add(ent);
                mTypeDict[type] = ent;
            }
            return(ent);
        }