private object Convert(object value) { foreach (ObjectLookupItemBase LookupItem in this.LookupItems) { if (LookupItem is ObjectLookupOtherwiseItem) { return(LookupItem.Value); } else if (LookupItem is ObjectLookupItem) { ObjectLookupItem Item = (ObjectLookupItem)LookupItem; Type KeyType = Item.Key?.GetType(); Type ValueType = value?.GetType(); if (KeyType == ValueType) { if (object.Equals(Item.Key, value)) { return(LookupItem.Value); } } else { if (object.Equals(Item.Key?.ToString(), value?.ToString())) { return(LookupItem.Value); } //TODO: implement dynamic casting; it's hard here because TypeDescriptor infrastructure is not available } } else { throw new InvalidOperationException("Unknown lookup item type"); } } //Not found - Fallback: return unconverted value return(value); }
private object ConvertBack(object value) { foreach (ObjectLookupItemBase LookupItem in this.LookupItems) { if (LookupItem is ObjectLookupOtherwiseItem) { return(null); } else if (LookupItem is ObjectLookupItem) { ObjectLookupItem Item = (ObjectLookupItem)LookupItem; Type DestinationType = Item.Value?.GetType(); Type ValueType = value?.GetType(); if (DestinationType == ValueType) { if (object.Equals(Item.Value, value)) { return(Item.Key); } } else { throw new NotImplementedException("TODO"); //TODO: implement dynamic casting } } else { throw new InvalidOperationException("Unknown lookup item type"); } } //Not found - Fallback: return unconverted value return(value); }