예제 #1
0
        public IdRefTypeConverter(WarbendService warbend, Type baseType, string path)
        {
            this.warbend = warbend;
            BaseType     = baseType;
            Path         = path;

            standardValues = new Lazy <StandardValuesCollection>(() => {
                var resp = warbend.GetArrayInfoAsync(Path).GetAwaiter().GetResult();
                var size = resp.Value <int>("size");
                return(new StandardValuesCollection(Enumerable.Range(0, size).ToArray()));
            });
        }
예제 #2
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
            {
                return("");
            }

            var index  = ((IConvertible)value).ToInt32(culture);
            var resp   = warbend.GetArrayInfoAsync(Path).GetAwaiter().GetResult();
            var keys   = resp.Value <JObject>("keys");
            var labels = resp.Value <JArray>("names");

            string text = null;

            if (!Settings.Default.UseRawIds)
            {
                if (index >= 0 && index < labels.Count)
                {
                    text = labels.Value <string>(index);
                }
            }
            if (text == null)
            {
                foreach (var kv in keys)
                {
                    if (kv.Value.Value <int>() == index)
                    {
                        text = kv.Key;
                        break;
                    }
                }
            }
            var fmt = "{0}" + (text == null ? "" : " ({1})");

            return(string.Format(culture, fmt, value, text));
        }