Пример #1
0
        private static BidirectionalDictionary <string, string> InitializeEnumType(Type type)
        {
            BidirectionalDictionary <string, string> map = new BidirectionalDictionary <string, string>(
                StringComparer.OrdinalIgnoreCase,
                StringComparer.OrdinalIgnoreCase);

            foreach (FieldInfo f in type.GetFields())
            {
                string n1 = f.Name;
                string n2;

#if !NET20
                n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                     .Cast <EnumMemberAttribute>()
                     .Select(a => a.Value)
                     .SingleOrDefault() ?? f.Name;
#else
                n2 = f.Name;
#endif

                string s;
                if (map.TryGetBySecond(n2, out s))
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, n2, type.Name));
                }

                map.Set(n1, n2);
            }

            return(map);
        }
Пример #2
0
        public BidirectionalDictionary <string, string> InitializeEnumType(Type type)
        {
            BidirectionalDictionary <string, string> map = XStandardCollections.Api.BidirectionalDictionaries.New <string, string>(StringComparer.Ordinal, StringComparer.Ordinal);

            foreach (FieldInfo f in type.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                string n1 = f.Name;
                string n2;

#if HAVE_DATA_CONTRACTS
                n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                     .Cast <EnumMemberAttribute>()
                     .Select(a => a.Value)
                     .SingleOrDefault() ?? f.Name;
#else
                n2 = f.Name;
#endif

                string s;
                if (map.TryGetBySecond(n2, out s))
                {
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, n2, type.Name));
                }

                map.Set(n1, n2);
            }

            return(map);
        }
Пример #3
0
        private BidirectionalDictionary <string, string> GetEnumNameMap(Type t)
        {
            BidirectionalDictionary <string, string> bidirectionalDictionary;

            if (!this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
            {
                lock (this._enumMemberNamesPerType)
                {
                    if (this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
                    {
                        return(bidirectionalDictionary);
                    }
                    bidirectionalDictionary = new BidirectionalDictionary <string, string>((IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase, (IEqualityComparer <string>)StringComparer.OrdinalIgnoreCase);
                    foreach (FieldInfo item_0 in t.GetFields())
                    {
                        string local_2 = item_0.Name;
                        string local_3 = item_0.Name;
                        string local_4;
                        if (bidirectionalDictionary.TryGetBySecond(local_3, out local_4))
                        {
                            throw new InvalidOperationException(StringUtils.FormatWith("Enum name '{0}' already exists on enum '{1}'.", (IFormatProvider)CultureInfo.InvariantCulture, (object)local_3, (object)t.Name));
                        }
                        bidirectionalDictionary.Set(local_2, local_3);
                    }
                    this._enumMemberNamesPerType[t] = bidirectionalDictionary;
                }
            }
            return(bidirectionalDictionary);
        }
Пример #4
0
        private BidirectionalDictionary <string, string> GetEnumNameMap(Type t)
        {
            BidirectionalDictionary <string, string> bidirectionalDictionary;

            if (!this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
            {
                lock (this._enumMemberNamesPerType)
                {
                    if (this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
                    {
                        return(bidirectionalDictionary);
                    }
                    bidirectionalDictionary = new BidirectionalDictionary <string, string>(StringComparer.OrdinalIgnoreCase, StringComparer.OrdinalIgnoreCase);
                    FieldInfo[] fields = t.GetFields();
                    for (int i = 0; i < fields.Length; i++)
                    {
                        FieldInfo fieldInfo = fields[i];
                        string    name      = fieldInfo.Name;
                        string    text      = (from EnumMemberAttribute a in fieldInfo.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                                               select a.Value).SingleOrDefault <string>() ?? fieldInfo.Name;
                        string text2;
                        if (bidirectionalDictionary.TryGetBySecond(text, out text2))
                        {
                            throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, text, t.Name));
                        }
                        bidirectionalDictionary.Set(name, text);
                    }
                    this._enumMemberNamesPerType[t] = bidirectionalDictionary;
                }
                return(bidirectionalDictionary);
            }
            return(bidirectionalDictionary);
        }
Пример #5
0
        public string GetReference(object context, object value)
        {
            BidirectionalDictionary <string, object> mappings = this.GetMappings(context);

            if (!mappings.TryGetBySecond(value, out string str))
            {
                this._referenceCount++;
                str = this._referenceCount.ToString(CultureInfo.InvariantCulture);
                mappings.Set(str, value);
            }
            return(str);
        }
Пример #6
0
        public string GetReference(object context, object value)
        {
            BidirectionalDictionary <string, object> mappings = this.GetMappings(context);
            string first;

            if (!mappings.TryGetBySecond(value, out first))
            {
                ++this._referenceCount;
                first = this._referenceCount.ToString((IFormatProvider)CultureInfo.InvariantCulture);
                mappings.Set(first, value);
            }
            return(first);
        }
        public string GetReference(object context, object value)
        {
            BidirectionalDictionary <string, object> mappings = GetMappings(context);

            string reference;

            if (!mappings.TryGetBySecond(value, out reference))
            {
                _referenceCount++;
                reference = _referenceCount.ToString(CultureInfo.InvariantCulture);
                mappings.Set(reference, value);
            }

            return(reference);
        }
Пример #8
0
        private BidirectionalDictionary <string, string> GetEnumNameMap(Type t)
        {
            BidirectionalDictionary <string, string> map;

            if (!_enumMemberNamesPerType.TryGetValue(t, out map))
            {
                lock (_enumMemberNamesPerType)
                {
                    if (_enumMemberNamesPerType.TryGetValue(t, out map))
                    {
                        return(map);
                    }

                    map = new BidirectionalDictionary <string, string>(
                        StringComparer.OrdinalIgnoreCase,
                        StringComparer.OrdinalIgnoreCase);

                    foreach (FieldInfo f in t.GetFields())
                    {
                        string n1 = f.Name;
                        string n2;

#if !NET20 && !MONO
                        n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                             .Cast <EnumMemberAttribute>()
                             .Select(a => a.Value)
                             .SingleOrDefault() ?? f.Name;
#else
                        n2 = f.Name;
#endif

                        string s;
                        if (map.TryGetBySecond(n2, out s))
                        {
                            throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'."
                                                                .FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
                        }

                        map.Set(n1, n2);
                    }

                    _enumMemberNamesPerType[t] = map;
                }
            }

            return(map);
        }
Пример #9
0
    /// <summary>
    /// A cached representation of the Enum string representation to respect per Enum field name.
    /// </summary>
    /// <param name="t">The type of the Enum.</param>
    /// <returns>A map of enum field name to either the field name, or the configured enum member name (<see cref="EnumMemberAttribute"/>).</returns>
    private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
    {
      BidirectionalDictionary<string, string> map;

      if (!_enumMemberNamesPerType.TryGetValue(t, out map))
      {
        lock (_enumMemberNamesPerType)
        {
          if (_enumMemberNamesPerType.TryGetValue(t, out map))
            return map;

          map = new BidirectionalDictionary<string, string>(
            StringComparer.OrdinalIgnoreCase,
            StringComparer.OrdinalIgnoreCase);

          foreach (FieldInfo f in t.GetFields())
          {
            string n1 = f.Name;
            string n2;
            
#if !NET20
            n2 = f.GetCustomAttributes(typeof (EnumMemberAttribute), true)
                          .Cast<EnumMemberAttribute>()
                          .Select(a => a.Value)
                          .SingleOrDefault() ?? f.Name;
#else
            n2 = f.Name;
#endif

            string s;
            if (map.TryGetBySecond(n2, out s))
            {
              throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'."
                .FormatWith(CultureInfo.InvariantCulture, n2, t.Name));
            }

            map.Set(n1, n2);
          }

          _enumMemberNamesPerType[t] = map;
        }
      }

      return map;
    }
        private static BidirectionalDictionary<string, string> InitializeEnumType(Type type)
        {
            BidirectionalDictionary<string, string> map = new BidirectionalDictionary<string, string>(
                StringComparer.OrdinalIgnoreCase,
                StringComparer.OrdinalIgnoreCase);

            foreach (FieldInfo f in type.GetFields())
            {
                string n1 = f.Name;
                string n2;

#if !NET20
                n2 = f.GetCustomAttributes(typeof(EnumMemberAttribute), true)
                    .Cast<EnumMemberAttribute>()
                    .Select(a => a.Value)
                    .SingleOrDefault() ?? f.Name;
#else
                n2 = f.Name;
#endif

                string s;
                if (map.TryGetBySecond(n2, out s))
                    throw new InvalidOperationException("Enum name '{0}' already exists on enum '{1}'.".FormatWith(CultureInfo.InvariantCulture, n2, type.Name));

                map.Set(n1, n2);
            }

            return map;
        }
Пример #11
0
 private BidirectionalDictionary<string, string> GetEnumNameMap(Type t)
 {
   BidirectionalDictionary<string, string> bidirectionalDictionary;
   if (!this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
   {
     lock (this._enumMemberNamesPerType)
     {
       if (this._enumMemberNamesPerType.TryGetValue(t, out bidirectionalDictionary))
         return bidirectionalDictionary;
       bidirectionalDictionary = new BidirectionalDictionary<string, string>((IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase, (IEqualityComparer<string>) StringComparer.OrdinalIgnoreCase);
       foreach (FieldInfo item_0 in t.GetFields())
       {
         string local_2 = item_0.Name;
         string local_3 = item_0.Name;
         string local_4;
         if (bidirectionalDictionary.TryGetBySecond(local_3, out local_4))
           throw new InvalidOperationException(StringUtils.FormatWith("Enum name '{0}' already exists on enum '{1}'.", (IFormatProvider) CultureInfo.InvariantCulture, (object) local_3, (object) t.Name));
         bidirectionalDictionary.Set(local_2, local_3);
       }
       this._enumMemberNamesPerType[t] = bidirectionalDictionary;
     }
   }
   return bidirectionalDictionary;
 }