【问题标题】:I want to using Cecil to create an Action<,> , but i don`t know how add genericity parameter我想使用 Cecil 创建一个 Action<,> ,但我不知道如何添加泛型参数
【发布时间】:2021-09-01 04:25:34
【问题描述】:

我想在我的课堂上添加一个Action&lt;,&gt; 归档,使用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;

问候。

【问题讨论】:

  • 什么是“”?
  • 所以我期望的是:public Action OnDataModeTestStr2ValueChange;但是生成的:public Action OnDataModeTestStr2ValueChange;
  • 我不知道 mono.cecil 也不知道这里做了什么,但是:typeof(Action&lt;string, string&gt;)?
  • 是的,但是字符串是不确定的类型,可以是任何类型,比如int,long……类型是form tmpFieldDefinition,我不确定。

标签: c# delegates action cil mono.cecil


【解决方案1】:

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

tmpAction = MakGenericInstanceType(new [] {TypeReference})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多