1、使用NuGet添加:Revit_All_Main_Versions_API_x64(动态引用)
2、修改工程文件配置:.csproj
复制如下的配置项
<PropertyGroup Condition="$(Configuration.Contains('2017'))">
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<RevitVersion>2017</RevitVersion>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('2018'))">
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<RevitVersion>2018</RevitVersion>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('2019'))">
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<RevitVersion>2019</RevitVersion>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('2020'))">
<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
<RevitVersion>2020</RevitVersion>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('2021'))">
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<RevitVersion>2021</RevitVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2017|x64'">
<OutputPath>bin\Debug2017\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;REVIT2017</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2018|x64'">
<OutputPath>bin\Debug2018\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;REVIT2018</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2019|x64'">
<OutputPath>bin\Debug2019\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;REVIT2019</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2020|x64'">
<OutputPath>bin\Debug2020\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;REVIT2020</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug2021|x64'">
<OutputPath>bin\Debug2021\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<Optimize>false</Optimize>
<DefineConstants>TRACE;DEBUG;REVIT2021</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2017|x64'">
<OutputPath>bin\Release2017\</OutputPath>
<Optimize>true</Optimize>
<DefineConstants>REVIT2017</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2018|x64'">
<OutputPath>bin\Release2018\</OutputPath>
<Optimize>true</Optimize>
<DefineConstants>REVIT2018</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2019|x64'">
<OutputPath>bin\Release2019\</OutputPath>
<Optimize>true</Optimize>
<DefineConstants>REVIT2019</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2020|x64'">
<OutputPath>bin\Release2020\</OutputPath>
<Optimize>true</Optimize>
<DefineConstants>REVIT2020</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release2021|x64'">
<OutputPath>bin\Release2021\</OutputPath>
<Optimize>true</Optimize>
<DefineConstants>REVIT2021</DefineConstants>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.3</Version>
</PackageReference>
<PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).0.0" />
</ItemGroup>
重新打开VS,选择一个版本,重新生成
此时去工程目录下查看就已添加了revit api那些dll
在coding 时,使用对应版本的条件编译符号进行区分每个版本对应不同的api 即可