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
        private MethodInfo GetConversionMethod(CastingOperatorType castingOperatorType, Type sourceType, Type targetType)
        {
            MethodInfo targetFromSource;
            MethodInfo sourceToTarget;

            // First search the target type for a suitable conversion method.

            targetFromSource = GetConversionMethod(OperatorMethodCache.GetOperatorMethods(targetType, castingOperatorType), sourceType, targetType);

            // Secondly search the source type for a suitable conversion method.

            sourceToTarget = GetConversionMethod(OperatorMethodCache.GetOperatorMethods(sourceType, castingOperatorType), sourceType, targetType);

            if (sourceToTarget == null && targetFromSource == null)
            {
                // Ok, no custom casting operators found. Look in the built-in conversions.

                return(BuiltInConversions.GetConversion(castingOperatorType, sourceType, targetType));
            }
            else
            {
                // Now we might have two possible methods. We found an ambiguous match in that case.

                if (targetFromSource != sourceToTarget && targetFromSource != null && sourceToTarget != null)
                {
                    // Ambiguous match, we don't know what to do.
                    _errorReporter.AmbiguousOperator(castingOperatorType, targetFromSource, sourceToTarget);
                    return(targetFromSource);
                }

                if (targetFromSource != null)
                {
                    return(targetFromSource);
                }
                else
                {
                    return(sourceToTarget);
                }
            }
        }
        public static MethodInfo[] GetOperatorMethods(Type type, CastingOperatorType op)
        {
            string opMethodName;

            if (op == CastingOperatorType.Implicit)
            {
                opMethodName = OP_IMPLICIT_METHOD_NAME;
            }
            else
            {
                opMethodName = OP_EXPLICIT_METHOD_NAME;
            }

            OperatorEntry entry = GetOperatorEntry(type);

            List <MethodInfo> methods;

            if (!entry.OperatorMethods.TryGetValue(opMethodName, out methods))
            {
                return(new MethodInfo[0]);
            }

            return(methods.ToArray());
        }
Beispiel #5
0
		public static MethodInfo[] GetOperatorMethods(Type type, CastingOperatorType op)
		{
			string opMethodName;

			if (op == CastingOperatorType.Implicit)
				opMethodName = OP_IMPLICIT_METHOD_NAME;
			else
				opMethodName = OP_EXPLICIT_METHOD_NAME;

			OperatorEntry entry = GetOperatorEntry(type);

            List<MethodInfo> methods;
		    if (!entry.OperatorMethods.TryGetValue(opMethodName, out methods))
				return new MethodInfo[0];

			return methods.ToArray();
		}
Beispiel #6
0
 void IErrorReporter.AmbiguousOperator(CastingOperatorType castingOperatorType, MethodInfo targetFromSource, MethodInfo sourceToTarget)
 {
     string message = String.Format(CultureInfo.CurrentCulture, Resources.AmbiguousCastingOperator, castingOperatorType, FormattingHelpers.FormatMethodInfo(targetFromSource), FormattingHelpers.FormatMethodInfo(sourceToTarget));
     HandleError(ErrorId.AmbiguousCastingOperator, message);
 }
Beispiel #7
0
		private MethodInfo GetConversionMethod(CastingOperatorType castingOperatorType, Type sourceType, Type targetType)
		{
			MethodInfo targetFromSource;
			MethodInfo sourceToTarget;

			// First search the target type for a suitable conversion method.

			targetFromSource = GetConversionMethod(OperatorMethodCache.GetOperatorMethods(targetType, castingOperatorType), sourceType, targetType);

			// Secondly search the source type for a suitable conversion method.

			sourceToTarget = GetConversionMethod(OperatorMethodCache.GetOperatorMethods(sourceType, castingOperatorType), sourceType, targetType);
	
			if (sourceToTarget == null && targetFromSource == null)
			{
				// Ok, no custom casting operators found. Look in the built-in conversions.

				return BuiltInConversions.GetConversion(castingOperatorType, sourceType, targetType);
			}
			else
			{
				// Now we might have two possible methods. We found an ambiguous match in that case.

				if (targetFromSource != sourceToTarget && targetFromSource != null && sourceToTarget != null)
				{
					// Ambiguous match, we don't know what to do.
					_errorReporter.AmbiguousOperator(castingOperatorType, targetFromSource, sourceToTarget);
					return targetFromSource;
				}

				if (targetFromSource != null)
					return targetFromSource;
				else
					return sourceToTarget;
			}
		}
Beispiel #8
0
        void IErrorReporter.AmbiguousOperator(CastingOperatorType castingOperatorType, MethodInfo targetFromSource, MethodInfo sourceToTarget)
        {
            string message = String.Format(CultureInfo.CurrentCulture, Resources.AmbiguousCastingOperator, castingOperatorType, FormattingHelpers.FormatMethodInfo(targetFromSource), FormattingHelpers.FormatMethodInfo(sourceToTarget));

            HandleError(ErrorId.AmbiguousCastingOperator, message);
        }