代码之家  ›  专栏  ›  技术社区  ›  florin

.NET多目标出错?

  •  0
  • florin  · 技术社区  · 16 年前

    我在vs 2008中有一个小的虚拟项目,只包含以下代码文件

    using System;
    
    namespace FrameworkTest
    {
        internal static class MessageQueueNative
        {
            struct TestStructure
            {
                public IntPtr aStatus;
            }
    
            public static void Main()
            {
                TestStructure pMgmtProps = new TestStructure { aStatus = IntPtr.Zero };
            }
        }
    }
    

    项目被设置为目标框架2.0,甚至在项目中手动将工具版本设置为2.0。现在,该项目正在从vs构建,并从命令行失败(使用csc.exe 2.0)。

    C:\windows\microsoft.net\framework\v2.0.50727\csc.exe/noconfig/nowarn:17011702/errorreport:prompt/warn:4/define:debug;trace/reference:C:\windows\microsoft.net\framework\v2.0.50727\system.data.dll/reference:C:\windows\microsoft.net\framework\v2.0.50727\system.dll/reference:C:\windows\microsoft.net\framework\v2.0.50727\system.xml.dll/debug+/调试:完整/fileAlign:512/optimize-/out:obj\debug\frameworkstest.exe/target:exe class1.cs properties\assemblyinfo.cs

    现在的问题是为什么它是从vs编译的?它应该像编译命令行那样失败。 同一个源在VS2005项目中失败(正确)。

    谢谢, 弗洛林

    4 回复  |  直到 16 年前
        1
  •  3
  •   Michael Damatov    16 年前

    多目标手段 CLR -版本目标,而不是 C.* -版本目标。

        2
  •  1
  •   Rory    16 年前

    您使用的是C 3编译器功能(teststructure的类型初始值设定项),这就是为什么代码不使用命令行(您使用的是v2)或vs2005中的csc进行编译。我猜VS2008使用的是C编译器的v3,不管您要针对哪个版本的框架,它只会禁用.NET框架的较新版本所支持的功能。

    如果你改变

    TestStructure pMgmtProps = new TestStructure { aStatus = IntPtr.Zero };
    

    到

    TestStructure pMgmtProps = new TestStructure();
    pMgmtProps.aStatus = IntPtr.Zero;
    

    它应该使用所有版本进行编译。不过看起来像个虫子…有趣的发现…

        3
  •  1
  •   ConsultUtah    16 年前

    这样的初始值设定项是C 3.0 编译程序 特征,而不是 .NET平台 特征。这是设计出来的。

        4
  •  1
  •   Lucero    16 年前

    VS2008总是使用C_3编译器,它允许您使用较新的语法(在示例中是初始值设定项)。由于这纯粹是语言的语法,编译器仍然可以以框架的v2为目标,而这正是目标实际所做的。您将能够在仅具有框架v2的计算机上运行生成的输出。

    使用vs2008中的msbuild来构建解决方案,您将得到一致的结果。

    推荐文章