我想使用 Cecil 创建一个 Action<,> ,但我不知道如何添加泛型参数

问题描述

我想使用 Action<,> 在我的班级创建中添加一个 Mono.cecill

但我无法理解这种不确定性。

我如何创建它?

我是这样写的,但写错了:

var tmpFieldDeFinition = (FieldDeFinition) methodAttributes.Body.Instructions[2].Operand;

TypeReference tmpAction = module.ImportReference(typeof(Action<,>));

var gp = new GenericParameter(tmpFieldDeFinition.FieldType);

tmpAction.GenericParameters[0] = gp;
tmpAction.GenericParameters[1] = gp;

CreateAngValueChangeEventField(typeDeFinition,$"{name}ValueChange",tmpAction);

我期望的是:

public Action<string,string> OnDataModeTestStr2ValueChange;

但是生成的:

public Action<,> OnDataModeTestStr2ValueChange;

问候。

解决方法

导入程序集参考 Mono.Cecil.Rocks ,之后 TypeReference tmpAction = module.ImportReference(typeof(Action<,>)) 然后使用

tmpAction = MakGenericInstanceType(new [] {TypeReference})