예제 #1
0
파일: Union.cs 프로젝트: bryanedds/Sigma
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == pointType)
     {
         return(value);
     }
     if (destinationType == typeof(string))
     {
         return(ConvertTo(context, culture, value, typeof(Symbol)).ToString());
     }
     if (destinationType == typeof(Symbol))
     {
         var tagField      = pointType.GetField(nameof(Union <bool, bool> .Tag));
         var tag           = (Enum)tagField.GetValue(value);
         var tagConverter  = new SymbolicConverter(tagField.FieldType);
         var tagSymbol     = new Symbol(tagConverter.ConvertToString(tag));
         var dataField     = pointType.GetField(nameof(Union <bool, bool> .Data));
         var data          = dataField.GetValue(value);
         var dataConverter = new SymbolicConverter(tag.TryGetAttributeOfType <UnionAttribute>().TryThen(attr => attr.Type) ?? dataField.FieldType);
         var dataSymbol    = dataConverter.CanConvertTo(typeof(Symbol)) ? (Symbol)dataConverter.ConvertTo(data, typeof(Symbol)) : new Symbol(dataConverter.ConvertToString(data));
         return(new Symbol(new List <Symbol>()
         {
             tagSymbol, dataSymbol
         }));
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
예제 #2
0
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if (destinationType == pointType)
     {
         return(value);
     }
     if (destinationType == typeof(string))
     {
         return(ConvertTo(context, culture, value, typeof(Symbol)).ToString());
     }
     if (destinationType == typeof(Symbol))
     {
         var recordType   = pointType.GetConcreteTypes().Where(type => type.Name.StartsWith("Record`")).First();
         var recordSize   = recordType.GetGenericArguments().Length;
         var fieldSymbols =
             Enumerable.Range(0, recordSize).Select(index =>
         {
             var fieldName      = "Item" + (index + 1);
             var field          = pointType.GetProperty(fieldName, BindingFlags.Public | BindingFlags.Instance);
             var fieldValue     = field.GetValue(value, null);
             var fieldConverter = new SymbolicConverter(field.PropertyType);
             return(fieldConverter.CanConvertTo(typeof(Symbol)) ? (Symbol)fieldConverter.ConvertTo(fieldValue, typeof(Symbol)) : new Symbol(fieldConverter.ConvertToString(fieldValue)));
         });
         return(new Symbol(fieldSymbols));
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }