使用ICorDebugEval

时间:2016-08-02 14:29:58

标签: c# generics clr cil imetadataimport

我正在使用MDBG示例制作托管.NET调试器。

MDBG不支持我试图添加的财产获取者评估。请考虑以下类结构:

    public abstract class Base<T>{
        public string SomeProp {get;set;}
    }

    public class A : Base<int>{

    }

在某些时候,我创建A的实例并在断点处停下来评估它的状态。

在我的调试器监视窗口中,我介绍&#34; this.SomeProp&#34;,它应该对此对象执行get_SomeProp方法的函数,并为给定的大小写返回null值。

我遇到的第一个问题是get_SomeProp是在基类上定义的,所以我必须运行类层次结构中的所有TypeDefs / TypeRefs / TypeSpec来查找函数。

但是在找到之后,打电话给

   ICorDebugEval.CallFunction(function.CorFunction, new[] {@object.CorValue});

导致: TypeLoadException:泛型类型在程序集中使用了错误数量的泛型参数。

我已经意识到这是因为非泛型函数是在泛型类(Base)中定义的,所以当我评估它时,我也应该指出类的通用参数。

这可以使用

完成
  ICorDebugEval2.CallParameterizedFunction(function.CorFunction,
    genericArguments,
    functionArguments);

问题是我不知道如何提取类通用参数的类型,只有我想要评估的函数和我要评估它的实例。

以下是我目前正在使用的一些代码:

    private MDbgValue EvaluatePropertyGetter(MDbgFrame scope, MDbgValue @object, string propertyName) {
        var propertyGetter = $"get_{propertyName}";
        var function = ResolveFunctionName(
            scope.Function.Module.CorModule.Name,
            @object.TypeName,
            propertyGetter,
            scope.Thread.CorThread.AppDomain);

        if (function == null) {
            throw new MDbgValueException("Function '" + propertyGetter + "' not found.");
        }

        var eval = Threads.Active.CorThread.CreateEval();
        var typeToken = function.CorFunction.Class.Token;
        var type = function.Module.Importer.GetType(typeToken); //checked that type containing function is generic
        if (type.IsGenericType) {
            //------------->need to get class'es generic param types<------------
            var genericType1 = this.ResolveType("System.Object"); // just a stub
            eval.CallParameterizedFunction(function.CorFunction, new CorType[] {genericType1}, new[] {@object.CorValue});
        }
        else {
            eval.CallFunction(function.CorFunction, new[] {@object.CorValue});
        }

        Go().WaitOne();
        if (!(StopReason is EvalCompleteStopReason)) {
            // we could have received also EvalExceptionStopReason but it's derived from EvalCompleteStopReason
            Console.WriteLine("Func-eval not fully completed and debuggee has stopped");
            Console.WriteLine("Result of funceval won't be printed when finished.");
        }
        else {
            eval = (StopReason as EvalCompleteStopReason).Eval;
            Debug.Assert(eval != null);

            var cv = eval.Result;
            if (cv != null) {
                var mv = new MDbgValue(this, cv);
                return mv;
            }
        }
        return null;
    }

非常感谢任何建议/意见!

此致

<小时/> 的解决方案

感谢@Brian Reichle出色的回答,我提出了这个解决方案:

 if (type.IsGenericType) {
            //getting Type's Generic parameters
            var typeParams = GetGenericArgs(@object.CorValue.ExactType, function.CorFunction.Class.Token);
            eval.CallParameterizedFunction(function.CorFunction, typeParams.ToArray(), new[] {@object.CorValue});
        }

功能本身:

 private List<CorType> GetGenericArgs(CorType corType, int classTk) {
        if (corType == null)
            return null;
        List<CorType> list = new List<CorType>();

        var param =corType.TypeParameters;
        var args = GetGenericArgs(corType.Base, classTk);

        if (classTk == corType.Class.Token) {
            list.AddRange(param.Cast<CorType>());
        }

        if (args != null) {
            list.AddRange(args);}

        return list;
    }

1 个答案:

答案 0 :(得分:2)

您可以在表示A实例的值对象上使用ICorDebugValue2::GetExactType来获取类型A的ICorDebugType,ICorDebugType::GetBase()以获取其基类({{1} }}和ICorDebugType::EnumerateTypeParameters在基类型上获取它的类型参数。