Beispiel #1
0
        public static MethodInfo GetConversion(CastingOperatorType castingOperatorType, Type sourceType, Type targetType)
        {
            EnsureTablesLoaded();

            Dictionary<ConversionKey, MethodInfo> conversionTable;
            switch (castingOperatorType)
            {
                case CastingOperatorType.Implicit:
                    conversionTable = _implicitConversionsTable;
                    break;
                case CastingOperatorType.Explicit:
                    conversionTable = _explicitConversionsTable;
                    break;
                default:
                    throw ExceptionBuilder.UnhandledCaseLabel(castingOperatorType);
            }

            ConversionKey key = new ConversionKey();
            key.SourceType = sourceType;
            key.TargetType = targetType;

            MethodInfo result;
            if (conversionTable.TryGetValue(key, out result))
                return result;

            return null;
        }
Beispiel #2
0
        public static MethodInfo GetConversion(CastingOperatorType castingOperatorType, Type sourceType, Type targetType)
        {
            EnsureTablesLoaded();

            Dictionary <ConversionKey, MethodInfo> conversionTable;

            switch (castingOperatorType)
            {
            case CastingOperatorType.Implicit:
                conversionTable = _implicitConversionsTable;
                break;

            case CastingOperatorType.Explicit:
                conversionTable = _explicitConversionsTable;
                break;

            default:
                throw ExceptionBuilder.UnhandledCaseLabel(castingOperatorType);
            }

            ConversionKey key = new ConversionKey();

            key.SourceType = sourceType;
            key.TargetType = targetType;

            MethodInfo result;

            if (conversionTable.TryGetValue(key, out result))
            {
                return(result);
            }

            return(null);
        }
Beispiel #3
0
        public static T ConvertNMSType <T>(object value)
        {
            ConversionKey key = ConversionKey.GetKey(typeof(T), value.GetType());

            NMSTypeConversionTable.TryGetValue(key, out ConversionEntry converter);
            if (converter == null)
            {
                throw new NMSTypeConversionException("Cannot convert between type : " + (typeof(T)).Name + ", and type: " + value.GetType().Name);
            }
            return((T)converter.Convert(value));
        }
Beispiel #4
0
        public static T ConvertNMSType <T, S>(S value)
        {
            ConversionKey          key       = ConversionKey.GetKey(typeof(T), value.GetType());
            ConversionEntry <T, S> converter = (ConversionEntry <T, S>)NMSTypeConversionTable[key];

            if (converter == null)
            {
                throw new NMSTypeConversionException("Cannot convert between type : " + (typeof(T)).Name + ", and type: " + value.GetType().Name);
            }
            return(converter.ConvertInstance(value));
        }
Beispiel #5
0
        private static void LoadConversionTable(IDictionary<ConversionKey, MethodInfo> table, Type type)
        {
            BindingFlags bindingFlags = BindingFlags.Public |
                                        BindingFlags.Static |
                                        BindingFlags.DeclaredOnly;

            MethodInfo[] methods = type.GetMethods(bindingFlags);
            Array.Sort(methods, (x, y) => String.Compare(x.ToString(), y.ToString(), StringComparison.Ordinal));

            foreach (MethodInfo methodInfo in methods)
            {
                ConversionKey key = new ConversionKey();
                key.SourceType = methodInfo.GetParameters()[0].ParameterType;
                key.TargetType = methodInfo.ReturnType;
                table.Add(key, methodInfo);
            }
        }
Beispiel #6
0
        private static void LoadConversionTable(IDictionary <ConversionKey, MethodInfo> table, Type type)
        {
            BindingFlags bindingFlags = BindingFlags.Public |
                                        BindingFlags.Static |
                                        BindingFlags.DeclaredOnly;

            MethodInfo[] methods = type.GetMethods(bindingFlags);
            Array.Sort(methods, (x, y) => String.Compare(x.ToString(), y.ToString(), StringComparison.Ordinal));

            foreach (MethodInfo methodInfo in methods)
            {
                ConversionKey key = new ConversionKey();
                key.SourceType = methodInfo.GetParameters()[0].ParameterType;
                key.TargetType = methodInfo.ReturnType;
                table.Add(key, methodInfo);
            }
        }
Beispiel #7
0
            public override bool Equals(object obj)
            {
                if (!(obj is ConversionKey))
                {
                    return(false);
                }

                ConversionKey conversionKey = (ConversionKey)obj;

                if (!Equals(SourceType, conversionKey.SourceType))
                {
                    return(false);
                }

                if (!Equals(TargetType, conversionKey.TargetType))
                {
                    return(false);
                }

                return(true);
            }
Beispiel #8
0
        public static bool CanConvertNMSType <T>(object value)
        {
            ConversionKey key = ConversionKey.GetKey(typeof(T), value.GetType());

            return(NMSTypeConversionSet.Contains(key));
        }
Beispiel #9
0
 protected virtual int CompareTo(ConversionKey other)
 {
     return(other.GetHashCode() - this.GetHashCode());
 }
Beispiel #10
0
 public static object Convert(object input, Type destinationType, object hint)
 {
     try
     {
         //log.DebugFormat( "converting [{0}] to [{1}] (null? {2}) empty? {3}",
         //                input, destinationType, input != null ? input.GetType().ToString() : "null", input == "" );
         if ( destinationType.IsInstanceOfType( input ) )
             return input;
         if (input == null )
             return null;
         object output = input;
         ConversionKey ck = new ConversionKey(input.GetType(),destinationType);
         if( converters.ContainsKey(ck) )
         {
             return converters[ck](input);
         }
         else if (destinationType.IsSubclassOf(typeof(AbstractRecord)) && !(output is AbstractRecord))
         {
             if (input == DBNull.Value || ( input is string && ((string)input == "NULL" || (string)input == string.Empty )))
                 output = null;
             else
             {
                 if( hint == null )
                 {
                     output = TypeLoader.InvokeGenericMethod( typeof(AbstractRecord), "Load", new Type[]{destinationType},null, new Type[]{typeof(object)}, new object[]{input} );
                 }
                 else
                 {
                     output = TypeLoader.InvokeGenericMethod( typeof(AbstractRecord), "LoadUsingRecord", new Type[]{destinationType}, null, new Type[]{destinationType, typeof(object)}, new object[]{hint,input}  );
                 }
             }
         }
         else if( destinationType == typeof(int) && input.GetType().IsSubclassOf(typeof(AbstractRecord) ) )
         {
             output = (int)input;
         }
         else if( destinationType == typeof(bool) && input is int )
         {
             output = ((int)input == 1);
         }
         else if( destinationType == typeof(bool) && input is System.SByte )
         {
             output = ((System.SByte)input == 1);
         }
         /*else if( destinationType == typeof(bool) && input is String )
         {
             log.Debug("converting to boolean");
             throw new Exception();
             output = ! string.IsNullOrEmpty(input as String);
         }
         else if( destinationType == typeof(bool) )
         {
             log.Debug("converting to boolean (non-string)");
             output = input != null;
         }*/
         else if (destinationType == typeof(int) && input.GetType().IsEnum)
         {
             output = System.Convert.ToInt16(input);
         }
         else if (destinationType.IsEnum && input is string)
         {
             output = Enum.Parse (destinationType, (string)input);
         }
         else if (destinationType == typeof(TimeSpan) && input is long)
         {
             output = new TimeSpan( (long)input );
         }
         else if (destinationType == typeof(DateTime) && input is int)
         {
             output = DateTime.SpecifyKind(System.Convert.ToDateTime(input), DateTimeKind.Utc);
         }
         else
         {
             TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
             if (!converter.CanConvertFrom(output.GetType()))
             {
                 output = output.ToString();
                 if( output.Equals("") )
                     return null;
             }
             try
             {
                 output = converter.ConvertFrom(output);
             }
             catch (Exception e)
             {
                 log.Error(string.Format("Error converting [{0}]({3}) to [{1}]({2})", input, destinationType, Util.BuildExceptionOutput( e ), input.GetType() ));
                 throw new Exception("error",e);
             }
         }
         return output;
      }
      catch( Exception e )
      {
      	log.Error("Error converting from %o (%s) to %s using hint %o: %s", input, input.GetType(), destinationType, hint, Util.BuildExceptionOutput(e) );
      	throw new Exception("error",e);
      }
 }
Beispiel #11
0
 public static void AddConverter( ConversionKey ck, Converter c )
 {
     log.Debug("Adding Converter ", ck.InputType, ck.OutputType );
     converters[ck] = c;
 }