Ejemplo n.º 1
0
 private static T DynamicResolveOperation(Unlimited <T> value1, object value2, string operationName)
 {
     Type[] genericTypeArguments = value1.GetType().GetTypeInfo().GenericTypeArguments;
     if (genericTypeArguments[0] == typeof(int))
     {
         int?num  = value1.Value as int?;
         int?num2 = value2 as int?;
         if (num2 == null)
         {
             num2 = new int?((int)Convert.ChangeType(value2, typeof(int)));
         }
         if (num2 == null)
         {
             throw new InvalidOperationException(DataStrings.ExceptionCannotResolveOperation(operationName, typeof(T).Name, value2.GetType().Name));
         }
         if ("op_Subtraction".Equals(operationName))
         {
             return((T)((object)(num.Value - num2.Value)));
         }
         if ("op_Division".Equals(operationName))
         {
             return((T)((object)(num.Value / num2.Value)));
         }
         if ("op_Multiply".Equals(operationName))
         {
             return((T)((object)(num.Value * num2.Value)));
         }
         return((T)((object)(num.Value + num2.Value)));
     }
     else if (genericTypeArguments[0] == typeof(uint))
     {
         uint?num3 = value1.Value as uint?;
         uint?num4 = value2 as uint?;
         if (num4 == null)
         {
             num4 = new uint?((uint)Convert.ChangeType(value2, typeof(uint)));
         }
         if (num4 == null)
         {
             throw new InvalidOperationException(DataStrings.ExceptionCannotResolveOperation(operationName, typeof(T).Name, value2.GetType().Name));
         }
         if ("op_Subtraction".Equals(operationName))
         {
             return((T)((object)(num3.Value - num4.Value)));
         }
         if ("op_Division".Equals(operationName))
         {
             return((T)((object)(num3.Value / num4.Value)));
         }
         if ("op_Multiply".Equals(operationName))
         {
             return((T)((object)(num3.Value * num4.Value)));
         }
         return((T)((object)(num3.Value + num4.Value)));
     }
     else
     {
         MethodInfo methodInfo = null;
         foreach (MethodInfo methodInfo2 in from x in genericTypeArguments[0].GetTypeInfo().DeclaredMethods
                  where x.Name == operationName
                  select x)
         {
             ParameterInfo[] parameters = methodInfo2.GetParameters();
             if (parameters.Length == 2 && parameters[0].ParameterType == genericTypeArguments[0] && parameters[1].ParameterType == value2.GetType())
             {
                 methodInfo = methodInfo2;
                 break;
             }
         }
         if (methodInfo == null)
         {
             throw new InvalidOperationException(DataStrings.ExceptionCannotResolveOperation(operationName, typeof(T).Name, value2.GetType().Name));
         }
         object[] parameters2 = new object[]
         {
             value1.Value,
             value2
         };
         T result;
         try
         {
             result = (T)((object)methodInfo.Invoke(value1.Value, parameters2));
         }
         catch (TargetInvocationException ex)
         {
             throw ex.InnerException;
         }
         return(result);
     }
 }