在CodeDom中生成以下代码。当我将目标框架设置为。net 4.0时,它工作得很好——没有错误或警告。当我将目标框架设置为。net 2.0时,我得到以下错误:
CS1526:一个新的表达式需要()或[]后类型
test soVar;
soVar = new test { foo = 0x10007 }; // Error occurs on this line
[StructLayout(LayoutKind.Sequential)]
struct test
{
public uint foo;
}
这是怎么回事?为什么切换到。net 2.0会突然引发一个错误?
期待你的建议。
谢谢,艾凡
test soVar;
soVar = new test { foo = 0x10007 }; // Error occurs on this line
.net 2.0不支持对象初始化式。它必须做
test soVar;
soVar = new test();
soVar.foo = 0x10007;
我正在运行Visual Studio 2010,并没有遇到您描述的问题。
我用创建了一个项目。. Net Framework 2.0为目标,程序编译:using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
[StructLayout(LayoutKind.Sequential)]
struct test
{
public uint foo;
}
class Program
{
static void Main(string[] args)
{
test soVar;
soVar = new test { foo = 0x10007 };
}
}
}
也许你正在使用 c# 2.0(例如Visual Studio 2005)而不是。Net 2.0 ?
要解决这个问题,请使用Bala的解决方案(不要使用对象初始化式)