Jochen Kühner
Jochen Kühner

Reputation: 1411

Add a Value Type independed

I have a Scriptable language, in which I can define different Script Elements. Now I want to include support for Mathematical Operators without having a explicit Type.

I think Code explains this better:

var ret = a + b;

where a and b are objects, but at runtime they are both of the same type (e.g. int, double, etc)...

How can I solve this, without specifying every Type?

Upvotes: 1

Views: 58

Answers (2)

Jochen Kühner
Jochen Kühner

Reputation: 1411

I now try it via

Expression.Add(...

I think this will work. I also get my Value of "a" by building a Property Get Expression...

Upvotes: 0

usr
usr

Reputation: 171178

a and b ar csharp varaibles of type object, so i can not add them, but in them is a typed wich can be added, but i know the type only at runtime

A perfect case for runtime binding:

dynamic ret = (dynamic)a + (dynamic)b;

This has about the same semantics as if a and b were statically typed with the exact runtime types.

Upvotes: 1

Related Questions