very-dynamic casting :-)

G

Giulio Petrucci

Hi everybody,

here's my problem: I have to dymanically build (and compile, of course)
some code, from some ECMAScript function. ECMAScript variables I get are
not typezed, so I should have operators like

variable == a string or a double ore anything else

in C# I thought to build a classes that wraps an object (untypized) and
typize it inside it and to overload operators. In the code below there's an
wxample with some questions among comments. Anyway the core-question is: in
a scenario like:

Variable v;
Object o;
....
v == o

how can I detect the type of "o" and try to performe a casting (note:
casting and not a conversion) of my variable v? and viceversa?

Thanks in advance,
Kind regards,
Giulio

--

public class ECMAVar
{
private Double _d;
private String _s;
private Boolean _b;
private object _o;
private Type innerType;
private bool isNull;


public ECMAVar(object o)
{
if (o == null)
isNull = true;
else isNull = false;

if (o is String)
{
if ((String)o == "undefined")
{
isNull = true;
_s = null;
_o = null;
}
else
{
_s = (String)o;
_o = _s;
}
/*
* handling dell'undefined;
*/
}
else
{
if ((o is Double)||(o is Int32))
{
_d = (Double)o;
_o = _d;
}
else
{
if (o is Boolean)
{
_b = (Boolean)o;
_o = _b;
}
else
{
/*
* eccezione;
*/
}
}
}
if (isNull == false)
innerType = _o.GetType();
else innerType = null;
}
public bool IsNull
{
get
{
return isNull;
}
}
public object InnerObject
{
get
{
return _o;
}
}

public static Boolean operator ==(ECMAVar apt, object o)
{
if (o is Int32)
o = Convert.ChangeType(o, TypeCode.Double);
if (apt.IsNull && (o == null))
return true;
if ((apt.IsNull && (o != null)) || (!apt.IsNull && (o == null)))
return false;
if (apt.InnerObject.GetType() == o.GetType())
return apt.InnerObject == o;
else
{
if (apt.InnerObject is System.String)
{
try
{
//question: how can I:
//cast apt.InnerObject into o.GetType()
//and retrurn the operator result?
}
catch (FormatException fe)
{
return false;
}
catch (InvalidCastException ice)
{
return false;
}
}
else
{
try
{
//question: how can I:
//cast o.GetType() into apt.InnerObject;
//and retrurn the operator result?
}
catch (InvalidCastException ice)
{
return false;
}
catch (FormatException fe)
{
return false;
}
}
}
}
}
 
B

Barry Kelly

here's my problem: I have to dymanically build (and compile, of course)
some code, from some ECMAScript function. ECMAScript variables I get are
not typezed, so I should have operators like

Are you using JScript.NET? It is (roughly) a superset of EcmaScript.
variable == a string or a double ore anything else

Does object.Equals(object,object) do what you want?

E.g.:

v == "foo" ==> object.Equals(v, "foo")

-- Barry
 
G

Giulio Petrucci

Barry Kelly ha scritto:
Does object.Equals(object,object) do what you want?

No, the problem is more complex.
If I have a string that's "3.2" it can be parsed as a double, so I can have:

Var v = new Var("3.2"); //Var is the wrapper class;
return (v==5.7)

or

return (v!="foo")

So I have to overload operators or one for strings, another for doubles...
and so on, or a single operator

public static Boolean operator(Var v, object o) { ... }

and put there the code to performe the operation. So I don't know if I'll
have to compare my Var object with a double or a string.

Thanks,
Giulio

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top