c# - 为类型创建指令

标签 c# il mono.cecil fody

使用 Mono.Cecil,给出这个方法

private Instruction LoadOnStack(MetadataType type, object value)
{
    switch (type)
    {
        case MetadataType.String:
            return _processor.Create(OpCodes.Ldstr, (string) value);
        case MetadataType.Int32:
            return _processor.Create(OpCodes.Ldc_I4, (Int32) value);
        case MetadataType.Int64:
            return _processor.Create(OpCodes.Ldc_I8, (Int64) value);
        case MetadataType.Boolean:
            return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);                
    }

    throw new NotSupportedException("Not a supported primitve parameter type: " + type);
}

value 的类型为 Type 时,如何创建可以加载 valueInstruction

我注意到当 valueType 类型时,我可以像这样测试它:

if (value is TypeReference)
    return _processor.Create(???, ???);

但我不知道我需要将什么传递给 Create 以获取正确加载的值。

编辑:

使用这个:

if (value is TypeReference)
    return _processor.Create(OpCodes.Ldobj, type.Resolve());

让我更近一步。它似乎接受类型。但是当我尝试编写程序集时,它会出错:

System.ArgumentException : Member 'System.Type' is declared in another module and needs to be imported

最佳答案

正如@cubrr 已经指出的那样:

我们将此代码用于 MethodBoundaryAspect.Fody

private IList<Instruction> LoadValueOnStack(TypeReference parameterType, object value, ModuleDefinition module)
{
    if (parameterType.IsPrimitive || (parameterType.FullName == "System.String"))
        return new List<Instruction> {LoadPrimitiveConstOnStack(parameterType.MetadataType, value)};

    if (parameterType.IsValueType) // enum
    {
        var enumUnderlyingType = GetEnumUnderlyingType(parameterType.Resolve());
        return new List<Instruction> {LoadPrimitiveConstOnStack(enumUnderlyingType.MetadataType, value)};
    }

    if (parameterType.FullName == "System.Type")
    {
        var typeName = value.ToString();
        var typeReference = module.GetType(typeName, true);

        var typeTypeRef = _referenceFinder.GetTypeReference(typeof (Type));
        var methodReference = _referenceFinder.GetMethodReference(typeTypeRef, md => md.Name == "GetTypeFromHandle");

        var instructions = new List<Instruction>
        {
            _processor.Create(OpCodes.Ldtoken, typeReference),
            _processor.Create(OpCodes.Call, methodReference)
        };

        return instructions;
    }

    throw new NotSupportedException("Parametertype: " + parameterType);
}

private Instruction LoadPrimitiveConstOnStack(MetadataType type, object value)
{
    switch (type)
    {
        case MetadataType.String:
            return _processor.Create(OpCodes.Ldstr, (string) value);
        case MetadataType.Int32:
            return _processor.Create(OpCodes.Ldc_I4, (int) value);
        case MetadataType.Int64:
            return _processor.Create(OpCodes.Ldc_I8, (long) value);
        case MetadataType.Boolean:
            return _processor.Create(OpCodes.Ldc_I4, (bool) value ? 1 : 0);
    }

    throw new NotSupportedException("Not a supported primitive parameter type: " + type);
}

private static TypeReference GetEnumUnderlyingType(TypeDefinition self)
{
    foreach (var field in self.Fields)
    {
        if (field.Name == "value__")
            return field.FieldType;
    }

    throw new ArgumentException();
} 

ReferenceFinder 类在哪里:

private readonly ModuleDefinition _moduleDefinition;

public ReferenceFinder(ModuleDefinition moduleDefinition)
{
    _moduleDefinition = moduleDefinition;
}

public MethodReference GetMethodReference(Type declaringType, Func<MethodDefinition, bool> predicate)
{
    return GetMethodReference(GetTypeReference(declaringType), predicate);
}

public MethodReference GetMethodReference(TypeReference typeReference, Func<MethodDefinition, bool> predicate)
{
    var typeDefinition = typeReference.Resolve();

    MethodDefinition methodDefinition;
    do
    {
        methodDefinition = typeDefinition.Methods.FirstOrDefault(predicate);
        typeDefinition = typeDefinition.BaseType == null 
            ? null 
            : typeDefinition.BaseType.Resolve();
    } while (methodDefinition == null && typeDefinition != null);

    return _moduleDefinition.Import(methodDefinition);
}

public TypeReference GetTypeReference(Type type)
{
    return _moduleDefinition.Import(type);
}

关于c# - 为类型创建指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35215430/

相关文章:

c# - Mono.Cecil:操作可能会在运行时不稳定

c# - Visual Studio 2012 中的 NuGet 构建失败

.net - 将 IL 注入(inject)强命名程序集?

c# - 如何翻译或转换 CompilerGenerated 代码?

.net - 为什么自定义属性同时出现在 IL 和 Metadata 中?

c# - 如何使用 mono.cecil 添加没有默认构造函数的自定义属性

c# - 带有可选参数的发射函数

c# - 托管与非托管

c# - 如何更改 ListView 的默认选择颜色?

c# - 事件参数中 IEnumerable 的最佳实践