[英]How to use CodeVariableDeclaraionStament to declare Arrays
CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement(p.ParameterType, p.Name,
new CodePrimitiveExpression(INTARR()));
考慮上面的代碼。 當我在調試模式下運行項目時,這些值將存儲在p.ParameterType和p.Name中。
p.ParameterType = {Name = "Int32[]" FullName = "System.Int32[]"}
p.Name = "x"
INTARR()是一種返回整數數組的方法。
但出現錯誤“無效的原始類型:System.Int32 []。請考慮使用CodeObjectCreateExpression。”
我怎樣才能對上面的代碼使用CodeObjectCreateExpression,即我想在CodeVariableDeclarationStatement中傳遞一個整數數組?
數組不是原始的。 您需要使用CodeArrayCreateExpression:
Int32[] ints = INTARR();
CodeExpression[] intExps = new CodePrimitiveExpression[ints.Length];
for (int i = 0; i < ints.Length; i++)
intExps[i] = new CodePrimitiveExpression(ints[i]);
CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement(
"Int32[]", "x", new CodeArrayCreateExpression("Int32", intExps));
基於此鏈接,我認為應該是這樣的
CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement(typeof(Int32[]), "x",
new CodePrimitiveExpression(INTARR()));
所以也許這樣?:
CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement(typeof(Int32[], "x",
new CodePrimitiveExpression(INTARR()));
p.ParameterType = typeof(Int32);
p.Name = "x";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.