public CorValue CreateCorValue(EvaluationContext ctx, CorType type, params CorValRef[] args) { CorEvaluationContext cctx = (CorEvaluationContext)ctx; CorValue[] vargs = new CorValue [args.Length]; for (int n = 0; n < args.Length; n++) { vargs [n] = args [n].Val; } Type t = type.GetTypeInfo(cctx.Session); MethodInfo ctor = null; foreach (MethodInfo met in t.GetMethods()) { if (met.IsSpecialName && met.Name == ".ctor") { ParameterInfo[] pinfos = met.GetParameters(); if (pinfos.Length == 1) { ctor = met; break; } } } if (ctor == null) { return(null); } CorFunction func = type.Class.Module.GetFunctionFromToken(ctor.MetadataToken); return(cctx.RuntimeInvoke(func, type.TypeParameters, null, vargs)); }
public override string GetTypeName(EvaluationContext ctx, object gtype) { CorType type = (CorType)gtype; CorEvaluationContext cctx = (CorEvaluationContext)ctx; Type t; if (CorMetadataImport.CoreTypes.TryGetValue(type.Type, out t)) { return(t.FullName); } try { if (type.Type == CorElementType.ELEMENT_TYPE_ARRAY || type.Type == CorElementType.ELEMENT_TYPE_SZARRAY) { return(GetTypeName(ctx, type.FirstTypeParameter) + "[" + new string (',', type.Rank - 1) + "]"); } if (type.Type == CorElementType.ELEMENT_TYPE_BYREF) { return(GetTypeName(ctx, type.FirstTypeParameter) + "&"); } if (type.Type == CorElementType.ELEMENT_TYPE_PTR) { return(GetTypeName(ctx, type.FirstTypeParameter) + "*"); } return(type.GetTypeInfo(cctx.Session).FullName); } catch (Exception ex) { Console.WriteLine(ex); throw; } }
MethodInfo OverloadResolve(CorEvaluationContext ctx, string methodName, CorType type, CorType[] argtypes, BindingFlags flags, bool throwIfNotFound) { List <MethodInfo> candidates = new List <MethodInfo> (); CorType currentType = type; while (currentType != null) { Type rtype = currentType.GetTypeInfo(ctx.Session); foreach (MethodInfo met in rtype.GetMethods(flags)) { if (met.Name == methodName || (!ctx.CaseSensitive && met.Name.Equals(methodName, StringComparison.CurrentCultureIgnoreCase))) { if (argtypes == null) { return(met); } ParameterInfo[] pars = met.GetParameters(); if (pars.Length == argtypes.Length) { candidates.Add(met); } } } if (methodName == ".ctor") { break; // Can't create objects using constructor from base classes } currentType = currentType.Base; } return(OverloadResolve(ctx, GetTypeName(ctx, type), methodName, argtypes, candidates, throwIfNotFound)); }
public override bool HasMember(EvaluationContext ctx, object tt, string memberName, BindingFlags bindingFlags) { CorEvaluationContext cctx = (CorEvaluationContext)ctx; CorType ct = (CorType)tt; while (ct != null) { Type type = ct.GetTypeInfo(cctx.Session); FieldInfo field = type.GetField(memberName, bindingFlags); if (field != null) { return(true); } PropertyInfo prop = type.GetProperty(memberName, bindingFlags); if (prop != null) { MethodInfo getter = prop.CanRead ? prop.GetGetMethod(bindingFlags.HasFlag(BindingFlags.NonPublic)) : null; if (getter != null) { return(true); } } if (bindingFlags.HasFlag(BindingFlags.DeclaredOnly)) { break; } ct = ct.Base; } return(false); }
public override ValueReference GetIndexerReference(EvaluationContext ctx, object target, object[] indices) { CorEvaluationContext cctx = (CorEvaluationContext)ctx; CorType targetType = GetValueType(ctx, target) as CorType; CorValRef[] values = new CorValRef[indices.Length]; CorType[] types = new CorType[indices.Length]; for (int n = 0; n < indices.Length; n++) { types[n] = (CorType)GetValueType(ctx, indices[n]); values[n] = (CorValRef)indices[n]; } List <MethodInfo> candidates = new List <MethodInfo> (); List <PropertyInfo> props = new List <PropertyInfo> (); List <CorType> propTypes = new List <CorType> (); CorType t = targetType; while (t != null) { Type type = t.GetTypeInfo(cctx.Session); foreach (PropertyInfo prop in type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { MethodInfo mi = null; try { mi = prop.CanRead ? prop.GetGetMethod() : null; } catch { // Ignore } if (mi != null && mi.GetParameters().Length > 0) { candidates.Add(mi); props.Add(prop); propTypes.Add(t); } } t = t.Base; } MethodInfo idx = OverloadResolve(cctx, GetTypeName(ctx, targetType), null, types, candidates, true); int i = candidates.IndexOf(idx); return(new PropertyReference(ctx, props[i], (CorValRef)target, propTypes[i], values)); }
public override IEnumerable <EnumMember> GetEnumMembers(EvaluationContext ctx, object tt) { CorType t = (CorType)tt; CorEvaluationContext cctx = (CorEvaluationContext)ctx; Type type = t.GetTypeInfo(cctx.Session); foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { if (field.IsLiteral && field.IsStatic) { object val = field.GetValue(null); EnumMember em = new EnumMember(); em.Value = (long)System.Convert.ChangeType(val, typeof(long)); em.Name = field.Name; yield return(em); } } }
protected override IEnumerable <ValueReference> GetMembers(EvaluationContext ctx, object tt, object gval, BindingFlags bindingFlags) { CorType t = (CorType)tt; CorValRef val = (CorValRef)gval; if (t.Class == null) { yield break; } CorEvaluationContext cctx = (CorEvaluationContext)ctx; while (t != null) { Type type = t.GetTypeInfo(cctx.Session); foreach (FieldInfo field in type.GetFields(bindingFlags)) { yield return(new FieldReference(ctx, val, t, field)); } foreach (PropertyInfo prop in type.GetProperties(bindingFlags)) { MethodInfo mi = null; try { mi = prop.CanRead ? prop.GetGetMethod() : null; } catch { // Ignore } if (mi != null && mi.GetParameters().Length == 0) { yield return(new PropertyReference(ctx, prop, val, t)); } } if ((bindingFlags & BindingFlags.DeclaredOnly) != 0) { break; } t = t.Base; } }
private bool IsCatchpoint(CorException2EventArgs e) { // Build up the exception type hierachy CorValue v = e.Thread.CurrentException; List <string> exceptions = new List <string>(); CorType t = v.ExactType; while (t != null) { exceptions.Add(t.GetTypeInfo(this).FullName); t = t.Base; } // See if a catchpoint is set for this exception. foreach (Catchpoint cp in Breakpoints.GetCatchpoints()) { if (cp.Enabled && exceptions.Contains(cp.ExceptionName)) { return(true); } } return(false); }
protected override TypeDisplayData OnGetTypeDisplayData(EvaluationContext ctx, object gtype) { CorType type = (CorType)gtype; CorEvaluationContext wctx = (CorEvaluationContext)ctx; Type t = type.GetTypeInfo(wctx.Session); if (t == null) { return(null); } string proxyType = null; string nameDisplayString = null; string typeDisplayString = null; string valueDisplayString = null; Dictionary <string, DebuggerBrowsableState> memberData = null; bool hasTypeData = false; foreach (object att in t.GetCustomAttributes(false)) { DebuggerTypeProxyAttribute patt = att as DebuggerTypeProxyAttribute; if (patt != null) { proxyType = patt.ProxyTypeName; hasTypeData = true; continue; } DebuggerDisplayAttribute datt = att as DebuggerDisplayAttribute; if (datt != null) { hasTypeData = true; nameDisplayString = datt.Name; typeDisplayString = datt.Type; valueDisplayString = datt.Value; continue; } } ArrayList mems = new ArrayList(); mems.AddRange(t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)); mems.AddRange(t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)); foreach (MemberInfo m in mems) { object[] atts = m.GetCustomAttributes(typeof(DebuggerBrowsableAttribute), false); if (atts.Length == 0) { atts = m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), false); if (atts.Length > 0) { atts[0] = new DebuggerBrowsableAttribute(DebuggerBrowsableState.Never); } } if (atts.Length > 0) { hasTypeData = true; if (memberData == null) { memberData = new Dictionary <string, DebuggerBrowsableState> (); } memberData[m.Name] = ((DebuggerBrowsableAttribute)atts[0]).State; } } if (hasTypeData) { return(new TypeDisplayData(proxyType, valueDisplayString, typeDisplayString, nameDisplayString, false, memberData)); } else { return(null); } }