反射中的错误类型转换发出

时间:2014-09-17 12:52:44

标签: .net c++-cli reflection.emit

我在C ++ / CLI中创建一个简单的编译器来生成一个简单的il代码......但是,当我尝试构建编译器时,编译器会返回这个错误:

1>------ Build started: Project: dnccpp, Configuration: Debug Win32 ------
1>  CodeGen.cpp
1>CodeGen.cpp(159): error C2664: 'void System::Reflection::Emit::ILGenerator::Emit(System::Reflection::Emit::OpCode,System::Reflection::Emit::LocalBuilder ^)' : cannot convert argument 2 from 'System::Int32 ^' to 'unsigned char'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
1>CodeGen.cpp(161): error C2664: 'void System::Reflection::Emit::ILGenerator::Emit(System::Reflection::Emit::OpCode,System::Reflection::Emit::LocalBuilder ^)' : cannot convert argument 2 from 'System::Double ^' to 'unsigned char'
1>          No user-defined-conversion operator available, or
1>          There is no context in which this conversion is possible
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========

我不能在参数中使用“unsigned char”,编译器会为此生成错误的代码......

void CodeGenerator::GenExpr(Expr^ expr, Type^ expectedType, ILGenerator^ il, array<Type^>^ argTypes)
{
    Type^ deliveredType;

    if (dynamic_cast<Literal^>(expr) != nullptr)
    {
        Object^ val = ((Literal^)expr)->value;
        deliveredType = val->GetType();
        if (dynamic_cast<String^>(val) != nullptr)
            il->Emit(OpCodes::Ldstr, (String^)val);
        else if (dynamic_cast<Int32^>(val) != nullptr)
            il->Emit(OpCodes::Ldc_I4, (int^)val);
        else if (dynamic_cast<Double^>(val) != nullptr)
            il->Emit(OpCodes::Ldc_R8, (double^)val);
    }
}

1 个答案:

答案 0 :(得分:2)

所有框架代码(包括Reflection Emit)都不使用强类型的盒装值。

^Int32^Double^double^中删除int^