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;
}
}
}
}
}