1

Can I get cast operators via reflection and invoke (or if there's a better name for that action) them? That's the main question, if yes, it solves my problem.

But if someone knows another solution to the following:

I have many object vars (unknown quantities until runtime), each one have a different type, but all of them are numbers (double, int, short).

On the other hand, I've got the same number of double vars, and need to compare values each to each.

object[] Value1; //this can be double, short or int, only known at runtime.
double[] Value2;

//I need simply to do:
(for int i = 0; i < Value1.Length, i++)
{
    (if Value1[i] == Value2[i]) //here's the problem
    //the comparison always return false, because of different types
    ....

I've tryed many things, including the "CompareTo" method invoked via reflection, but it demands that de object vars are of the same type of the instance containing the method invoked. (Cannot call from a double and pass an int parameter, nor call from int and pass a double parameter)

So, if I could just cast them: One way is from double to the unknown type (for that I'd have to use some reflection) The other way is a two step casting from object to the unknown type, and from that to double.

Limitations:

  • dynamic is not an option (old framework)

  • Value1 items come individually "as object" from an external unchangeable method.

1 Answer 1

1

Change Value1 to a double[]. If you have an object to put into it (instance of short, int, double, etc.), use Convert.ToDouble(value), or if you have a known short or int, it will be implicitly converted to a double. These can then be compared with the doubles in Value2.

Sign up to request clarification or add additional context in comments.

2 Comments

Forgot to tell: the Value1 items come from a method of another assembly (unchangeable). It returns the value as object (of type int, short, or double). That's why I can't simply put them into Value1. Would be the same as trying to cast object to double.
@Daniel, if the values for Value1 (not Value1 itself) come from an existing method, you could use Convert.ToDouble(value) on the doubles, shorts and ints, and force them to be doubles. Then Value1 could simply be of type double[] as Tim suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.