简体   繁体   中英

Get Explicit Cast operators via reflection

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.

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 double s in Value2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM