none
Help reflecting operator methods RRS feed

  • Question

  • Hi.
    I need to invoke the Addition operator in a given object (if exist) in order to perform a sum.
    The object parameters are System.Object types so i don't know if they really has the operator implemented, so i used reflection:

    public static object Add(object x, object y)
    {
    	var xType = x.GetType();
    	MethodInfo op = xType.GetMethod("op_Addition");
    	if (op == null)
    	{
    		throw new InvalidOperationException("Some error");
    	}
    	return op.Invoke(x, new object[] { x, y });
    }

    But what happens when the objects are primitive types like Int32? I red this article and they say "Int32 (and the other primitive types) don't have operator methods; they use specific CIL instructions instead."

    So my solution to the problem was verify if the type is a primitive type then use 'dynamic' keyword to call the method:

    public static object Add(object x, object y)
    {
    	dynamic dynX = (dynamic)x;
    	dynamic dynY = (dynamic)y;
    	return dynX + dynY;
    }

    I'm not very sure what happens there but it worked.

    What i'm asking is:

    1. Is there a better approach to my problem? I've seen solutions like parse the object to its proper type but that is no solution in my case. I have to use reflections because there are a lot of primitive types that can be added via the '+' operator.

    2. What actually happens when i parse the parameters using the 'dynamic' keyword? I'm worried about if solving my problem using this approach i'm creating a bigger one that can not be seen easily. I think this is no the scenario for what the 'dynamic' was added to the language.

    Thanks a lot.

    Saturday, May 17, 2014 6:52 AM

Answers

  • "1. Is there a better approach to my problem?"

    Not really. As you already mentioned, you could "parse the object to its proper type" but that's a lot of work. And if you need to add 2 values of different types, such as float + int, the amount of work needed to figure out the necessary conversion is even larger.

    "2. What actually happens when i parse the parameters using the 'dynamic' keyword?"

    A mini C# compiler is invoked at runtime to produce code that's equivalent to the code that would have been produced at compile time if the value types were known. Using dynamic is a valid approach, the only drawback of dynamic is that it is relatively slow. But it's not like you have a choice, using reflection and trying to deal with primitive types which don't have the necessary operators is also slow.

    The only better solution would be to avoid this situation completely. But since you're asking this I assume that's not actually possible. If you want/can try explaining why do you need this and maybe someone can provide a different alternative.

    Saturday, May 17, 2014 9:29 AM
    Moderator

All replies

  • Are you trying to read an XML file?  I don't like using reflection methods or general objects if it isn't required.  You may have an XML with a schema where everything is fully defined.

    jdweng

    Saturday, May 17, 2014 6:59 AM
  • "1. Is there a better approach to my problem?"

    Not really. As you already mentioned, you could "parse the object to its proper type" but that's a lot of work. And if you need to add 2 values of different types, such as float + int, the amount of work needed to figure out the necessary conversion is even larger.

    "2. What actually happens when i parse the parameters using the 'dynamic' keyword?"

    A mini C# compiler is invoked at runtime to produce code that's equivalent to the code that would have been produced at compile time if the value types were known. Using dynamic is a valid approach, the only drawback of dynamic is that it is relatively slow. But it's not like you have a choice, using reflection and trying to deal with primitive types which don't have the necessary operators is also slow.

    The only better solution would be to avoid this situation completely. But since you're asking this I assume that's not actually possible. If you want/can try explaining why do you need this and maybe someone can provide a different alternative.

    Saturday, May 17, 2014 9:29 AM
    Moderator
  • Hi Mike. Thanks a lot, i've clear all my problems now, or at least i know what actually hapens when my code runs.

    Thanks.

    Sunday, May 18, 2014 6:16 AM