C#CodeDom在类型之间转换

C#CodeDom在类型之间转换,c#,types,codedom,C#,Types,Codedom,我尝试使用CodeDom生成以下代码行: object o = (object)bytes 其中“bytes”表示字节数组:byte[]bytes=null 我可以使用VariableDeclaration方法,甚至可能使用CodeAssign方法来生成此行的左侧,但是如何创建此行的右侧 我愿意接受任何建议-谢谢 Evan这种转化形式称为铸造。转换是指类似于Convert.ToInt32(“123”),或int.Parse(“123”) Cast(您的确切行对象o=(对象)字节;) Conve

我尝试使用CodeDom生成以下代码行:

object o = (object)bytes
其中“bytes”表示字节数组:byte[]bytes=null

我可以使用VariableDeclaration方法,甚至可能使用CodeAssign方法来生成此行的左侧,但是如何创建此行的右侧

我愿意接受任何建议-谢谢


Evan

这种转化形式称为铸造。转换是指类似于
Convert.ToInt32(“123”)
,或
int.Parse(“123”)

Cast(您的确切行
对象o=(对象)字节;

Convert(我的转换示例
对象o=Convert.ToInt32(“123”)

var declaration = new CodeVariableDeclarationStatement()
{
    Name = "o",
    Type = new CodeTypeReference(typeof(object)),
    InitExpression = new CodeCastExpression(typeof(object), new CodeVariableReferenceExpression("bytes"))
};
var declaration = new CodeVariableDeclarationStatement()
{
    Name = "o",
    Type = new CodeTypeReference(typeof(object)),
    InitExpression = new CodeMethodInvokeExpression(
        new CodeTypeReferenceExpression(typeof(Convert)),
        "ToInt32",
        new CodePrimitiveExpression("123"))
};