示例#1
0
        public async Task <IFormattedValue> VisitDictionaryMatchFormatter(IUshortsFormatter formatter)
        {
            IChosenFromListValue         chosenFromListValue         = _typesContainer.Resolve <IChosenFromListValue>();
            IDictionaryMatchingFormatter dictionaryMatchingFormatter = formatter as IDictionaryMatchingFormatter;

            chosenFromListValue.InitList(dictionaryMatchingFormatter.StringDictionary.Values);

            if (!dictionaryMatchingFormatter.StringDictionary.Any((pair => pair.Key == _ushortsPayload[0])))
            {
                if (dictionaryMatchingFormatter.UseDefaultMessage)
                {
                    chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.DefaultMessage;
                    return(chosenFromListValue);
                }

                chosenFromListValue.SelectedItem = this._ushortsPayload[0].ToString();
                return(chosenFromListValue);
            }

            if (dictionaryMatchingFormatter.IsKeysAreNumbersOfBits)
            {
                BitArray bitArray      = new BitArray(new int[] { _ushortsPayload[0] });
                bool     isValueExists = false;
                for (ushort i = 0; i < bitArray.Length; i++)
                {
                    if ((bitArray[i]) && (dictionaryMatchingFormatter.StringDictionary.ContainsKey(i)))
                    {
                        chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[i];
                        isValueExists = true;
                    }
                }

                if ((!isValueExists) && (dictionaryMatchingFormatter.StringDictionary.ContainsKey(0)))
                {
                    chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[0];
                }
            }
            else
            {
                chosenFromListValue.SelectedItem = dictionaryMatchingFormatter.StringDictionary[_ushortsPayload[0]];
            }

            return(chosenFromListValue);
        }
        public async Task <ushort[]> VisitDictionaryMatchFormatter(IUshortsFormatter formatter)
        {
            IChosenFromListValue         chosenFromListValue         = _formattedValue as IChosenFromListValue;
            IDictionaryMatchingFormatter dictionaryMatchingFormatter = formatter as IDictionaryMatchingFormatter;

            ushort[] resultedUshorts = new ushort[1];
            if (dictionaryMatchingFormatter.IsKeysAreNumbersOfBits)
            {
                BitArray bitArray  = new BitArray(16);
                ushort   numberKey = dictionaryMatchingFormatter.StringDictionary
                                     .First((pair => pair.Value == chosenFromListValue.SelectedItem)).Key;
                bitArray[numberKey] = true;
                resultedUshorts[0]  = (ushort)bitArray.GetIntFromBitArray();
            }
            else
            {
                resultedUshorts[0] = dictionaryMatchingFormatter.StringDictionary
                                     .First((pair => pair.Value == chosenFromListValue.SelectedItem))
                                     .Key;
            }

            return(resultedUshorts);
        }