198
201
public abstract object[] GetTypeArgs (EvaluationContext ctx, object type);
199
202
public abstract object GetBaseType (EvaluationContext ctx, object type);
204
public virtual bool IsFlagsEnumType (EvaluationContext ctx, object type)
209
public virtual IEnumerable<EnumMember> GetEnumMembers (EvaluationContext ctx, object type)
211
object longType = GetType (ctx, "System.Int64");
212
TypeValueReference tref = new TypeValueReference (ctx, type);
213
foreach (ValueReference cr in tref.GetChildReferences ()) {
214
object c = Cast (ctx, cr.Value, longType);
215
long val = (long) TargetObjectToObject (ctx, c);
216
EnumMember em = new EnumMember () { Name = cr.Name, Value = val };
201
221
public object GetBaseType (EvaluationContext ctx, object type, bool includeObjectClass)
203
223
object bt = GetBaseType (ctx, type);
613
638
} else if (IsArray (ctx, obj)) {
614
639
ICollectionAdaptor adaptor = CreateArrayAdaptor (ctx, obj);
615
StringBuilder tn = new StringBuilder (GetDisplayTypeName (GetTypeName (ctx, adaptor.ElementType)));
640
string ename = GetDisplayTypeName (GetTypeName (ctx, adaptor.ElementType));
616
641
int[] dims = adaptor.GetDimensions ();
642
StringBuilder tn = new StringBuilder ("[");
618
643
for (int n=0; n<dims.Length; n++) {
621
646
tn.Append (dims[n]);
624
return new LiteralExp (tn.ToString ());
649
int i = ename.LastIndexOf ('>');
651
i = ename.IndexOf ('[', i);
653
return new EvaluationResult ("{" + ename.Substring (0, i) + tn + ename.Substring (i) + "}");
655
return new EvaluationResult ("{" + ename + tn + "}");
657
else if (IsEnum (ctx, obj)) {
658
object type = GetValueType (ctx, obj);
659
object longType = GetType (ctx, "System.Int64");
660
object c = Cast (ctx, obj, longType);
661
long val = (long) TargetObjectToObject (ctx, c);
663
string typeName = GetTypeName (ctx, type);
664
string composed = string.Empty;
665
string composedDisplay = string.Empty;
666
foreach (EnumMember em in GetEnumMembers (ctx, type)) {
668
return new EvaluationResult (typeName + "." + em.Name, em.Name);
670
if ((rest & em.Value) == em.Value) {
672
if (composed.Length > 0) {
674
composedDisplay += "|";
676
composed += typeName + "." + em.Name;
677
composedDisplay += em.Name;
681
if (IsFlagsEnumType (ctx, type) && rest == 0 && composed.Length > 0)
682
return new EvaluationResult (composed, composedDisplay);
684
return new EvaluationResult (val.ToString ());
686
else if (GetValueTypeName (ctx, obj) == "System.Decimal") {
687
string res = CallToString (ctx, obj);
688
// This returns the decimal formatted using the current culture. It has to be converted to invariant culture.
689
decimal dec = decimal.Parse (res);
690
res = dec.ToString (System.Globalization.CultureInfo.InvariantCulture);
691
return new EvaluationResult (res);
626
693
else if (IsClassInstance (ctx, obj)) {
627
694
TypeDisplayData tdata = GetTypeDisplayData (ctx, GetValueType (ctx, obj));
628
695
if (!string.IsNullOrEmpty (tdata.ValueDisplayString) && ctx.Options.AllowDisplayStringEvaluation)
629
return new LiteralExp (EvaluateDisplayString (ctx, obj, tdata.ValueDisplayString));
696
return new EvaluationResult (EvaluateDisplayString (ctx, obj, tdata.ValueDisplayString));
630
697
// Return the type name
631
698
if (ctx.Options.AllowToStringCalls)
632
return new LiteralExp ("{" + CallToString (ctx, obj) + "}");
699
return new EvaluationResult ("{" + CallToString (ctx, obj) + "}");
633
700
if (!string.IsNullOrEmpty (tdata.TypeDisplayString) && ctx.Options.AllowDisplayStringEvaluation)
634
return new LiteralExp ("{" + EvaluateDisplayString (ctx, obj, tdata.TypeDisplayString) + "}");
635
return new LiteralExp ("{" + GetDisplayTypeName (GetValueTypeName (ctx, obj)) + "}");
701
return new EvaluationResult ("{" + EvaluateDisplayString (ctx, obj, tdata.TypeDisplayString) + "}");
702
return new EvaluationResult ("{" + GetDisplayTypeName (GetValueTypeName (ctx, obj)) + "}");
637
return new LiteralExp ("{" + CallToString (ctx, obj) + "}");
704
return new EvaluationResult ("{" + CallToString (ctx, obj) + "}");
707
public object Convert (EvaluationContext ctx, object obj, object targetType)
711
object res = TryConvert (ctx, obj, targetType);
715
throw new EvaluatorException ("Can't convert an object of type '{0}' to type '{1}'", GetValueTypeName (ctx, obj), GetTypeName (ctx, targetType));
718
public virtual object TryConvert (EvaluationContext ctx, object obj, object targetType)
720
return TryCast (ctx, obj, targetType);
640
723
public virtual object Cast (EvaluationContext ctx, object obj, object targetType)
727
object res = TryCast (ctx, obj, targetType);
731
throw new EvaluatorException ("Can't cast an object of type '{0}' to type '{1}'", GetValueTypeName (ctx, obj), GetTypeName (ctx, targetType));
645
734
public virtual string CallToString (EvaluationContext ctx, object obj)