multi targeting in command line compiler (csc.exe)

前端 未结 2 1831
旧时难觅i
旧时难觅i 2021-01-24 12:19

My question is simple: How can I do multi-targeting in command line compiler (csc.exe), especially to .Net 4 Client Profile?


edit: Ok, Is my que

相关标签:
2条回答
  • 2021-01-24 12:59

    If you need to compile at runtime, then you should consider the providers in System.CodeDOM, which allow compilation without invoking a separate process.

    To answer your original question, if you turn the MSBuild verbosity to Detailed in Visual Studio (Options - Projects and Solutions - Build and Run) and build a project targeted at client profile, you will see this in the build output:

    Csc.exe (stuff...) Program.cs Properties\AssemblyInfo.cs "C:\...\Temp\.NETFramework,Version=v4.0,Profile=Client.AssemblyAttributes.cs"

    The path in quotes is actually a generated temp file, containing:

    [assembly: TargetFrameworkAttribute(".NETFramework,Version=v4.0,Profile=Client", FrameworkDisplayName = ".NET Framework 4 Client Profile")]

    So, you should be able to use that attribute in your own code if you are invoking csc directly.

    0 讨论(0)
  • 2021-01-24 13:03

    TargetFramework can be configured in the Project file only and can't be passed as a switch to CSC.exe, see settings for TargetFrameworkVersion and TargetFrameworkProfile in below example

    So the only way to dynamically set is to modify the project file with below setting and compile with csc.exe if you want to set Client Profile

    Targetting .NET Framework 4.0 Client Profile

    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{A5F58561-47CA-482A-83E0-1D43C312B0A7}</ProjectGuid>
        <OutputType>Exe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ConsoleApplication1</RootNamespace>
        <AssemblyName>ConsoleApplication1</AssemblyName>
        <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <TargetFrameworkProfile>Client</TargetFrameworkProfile>
      </PropertyGroup>
    

    Targetting .NET Framework 4.0

    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{A5F58561-47CA-482A-83E0-1D43C312B0A7}</ProjectGuid>
        <OutputType>Exe</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>ConsoleApplication1</RootNamespace>
        <AssemblyName>ConsoleApplication1</AssemblyName>
        <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <TargetFrameworkProfile></TargetFrameworkProfile>
      </PropertyGroup>
    
    0 讨论(0)
提交回复
热议问题