Yes, this topic has been discussed and solutions presented both here in this forum and elsewhere, e.g., on GitHub. Have you searched for any of those? I could do so for you, but I am lazy, so I prefer to ask Gemini instead, as I described in my blog. Here is the response I get:
Let's tackle this dual .NET framework Revit add-in challenge for Revit 2025 and earlier versions. You're on the right track wanting to avoid code duplication – maintaining two separate projects is a real headache. Here's a breakdown of the best approach, combining best practices and practical examples.
The Core Problem: .NET Version Conflicts
Revit 2025 requires .NET 8, while older versions rely on earlier .NET frameworks (e.g., .NET 4.8 for Revit 2024 and earlier). Directly compiling a .NET 8 add-in won't work with older Revit versions because they lack the necessary runtime.
The Solution: Conditional Compilation and Target Frameworks
The most elegant solution is to use a single project with conditional compilation and multiple target frameworks. This allows you to build one codebase that produces separate DLLs for different Revit versions.
1. Project Setup (Visual Studio)
- Create a New Revit Add-in Project: Start with a standard Revit add-in project template.
- Modify the Project File (.csproj): This is where the magic happens. You'll add multiple target frameworks and conditional compilation directives. Here's an example:
2. Conditional Compilation in Code
Now, use the defined preprocessor symbols (REVIT2024_OR_EARLIER
and REVIT2025_OR_LATER
) in your C# code to handle version-specific logic:
3. Revit API DLL Paths
The crucial part is setting up the $(RevitAPIDLLPath)
property. You can do this in a few ways:
- Environment Variable: The cleanest approach. Set a system environment variable named
RevitAPIDLLPath
to the directory containing the Revit API DLLs for each Revit version you want to support. Your code will then automatically pick up the correct path.
- Project Properties: You can define project-level properties in Visual Studio, but this is less flexible.
4. Build Process
When you build your project, Visual Studio will create separate DLLs in the specified output directories (bin\$(Configuration)\$(Platform)\net48\
and bin\$(Configuration)\$(Platform)\net8.0\
). You'll then need to distribute the correct DLL to the appropriate Revit version's AddIns folder.
5. Add-in Manifest (for Revit 2025)
For Revit 2025, your add-in manifest file will point to the .NET 8.0
DLL. For older Revit versions, you'll have a separate manifest pointing to the .NET 4.8
DLL. You can automate this process with a post-build event in your project file.
Key Advantages:
- Single Codebase: Maintainability is vastly improved.
- Conditional Compilation: Handle version differences elegantly.
- Organized Output: Separate DLLs for each Revit version.
Addressing Specific Questions:
- Open Source Template: While there isn't one single "official" template for this, the principles above are the standard approach. Many open-source Revit add-ins use this technique, so examining their
.csproj
files will be highly beneficial.
- No Code Duplication: You avoid duplicating your core logic. The conditional compilation handles the small version-specific tweaks.
This detailed approach should give you a robust and maintainable solution for your dual .NET framework Revit add-in. Remember to test thoroughly in all target Revit versions! Let me know if you have any more questions.
I hope this helps and leave it up to you to dive in and research deeper if desired or required. Good luck, and please share the final solution that you end up with. Thank you!