Is it possible to call value type operators via reflection?

前端 未结 5 1396
别跟我提以往
别跟我提以往 2020-12-09 04:54

As C# operators e.g. +, +=, == are overridable. It lets me think they are sort of methods, thus wonder if there is a way to call them using reflection, on Int32 for instance

相关标签:
5条回答
  • 2020-12-09 05:38

    What exactly is it you want to do? Dealing with the various meanings of operators (primitive (mapped to specific IL instructions), custom (mapped to static methods), and lifted (provided as a pattern by the compiler)) makes this painful. If you just want to use the operators, then it is possible to write code that provides operator support via generics. I have some code for this that is freely available in MiscUtil (description and examples).


    As an untyped example (an note that this isn't hugely efficient, but works):

    object x = 123, y = 345; // now forget that we know that these are ints...
    object result = Expression.Lambda<Func<object>>(
        Expression.Convert(Expression.Add(
            Expression.Constant(x), Expression.Constant(y)),
        typeof(object))).Compile()();
    
    0 讨论(0)
  • 2020-12-09 05:38

    There are good answers here, so just to add something not mentioned yet:

    A struct might have an operator overloaded. This means more options to check if you try to create some programmatic approach.

    One nice thing to try out is, to try the expression tree approach. Here's a small sample. Of course, performance isn't too nice, but we know what we're getting into with Reflection, anyway.

    0 讨论(0)
  • 2020-12-09 05:44

    Yes, the custom operators are invokable using reflection (they have special names, such as op_Addition), but System.Int32 doesn't define them, as fundamental, built-in, types are handled directly by IL opcodes like add, rather than method calls.

    0 讨论(0)
  • 2020-12-09 05:44

    What about this, it's simple, small and works :)

    public T Add<T>(object x, object y)
    {
        return (T)Convert.ChangeType((dynamic)x + (dynamic)y, typeof(T));
    }
    
    0 讨论(0)
  • 2020-12-09 06:00

    It would be very inefficient if adding or comparing two integers required a method call so these simple operations on the fundamental types are code-generated as explained in another answer and cannot be invoked using reflection. One interesting built-in value type is decimal (or System.Decimal). This struct has support for literal values in C#, but behaves a lot like a real value type and exposes a lot of operators that can be invoked via reflection.

    If you are curious you can use Reflector to browse all members exposed by System.Int32.

    0 讨论(0)
提交回复
热议问题