So the ExternalDefinitionCreationOptions had a spelling error in the Revit 2015 API that was corrected in the 2016 API.
I try to make my app as compatible as possible with the current version + previous, but this time I'm not even able to compile it since I can only reference one of the two API DLL's, and the ExternalDefinitionCreationOptions
plays a big role in the process.
The code is the following:
private static Definition GetSimpleParameterDefinition(UIApplication uiApp, Document doc, DefinitionGroup defGroup, string name)
{
var definition = defGroup.Definitions.FirstOrDefault(d => d.Name == name);
if (definition != null) return definition;
var parameterType = ParameterType.Text;
var defOptions = new ExternalDefinitionCreationOptions(name, parameterType);
BuiltInCategory target = BuiltInCategory.OST_Furniture;
var cat = doc.Settings.Categories.get_Item(target);
var catSet = uiApp.Application.Create.NewCategorySet();
catSet.Insert(cat);
definition = defGroup.Definitions.Create(defOptions);
return definition;
}
I'm reading about DI and IoC, but all the samples have all the code under control, not referencing a third-party API and dealing with it. I've run out of ideas.
Any thoughts on how to accomplish this?
Why not use conditional compilation?
You have to define a REVIT2015 conditional compilation symbol in your Revit 2015 project (Project options, Build tab).
Of course, this only work if you have two separate VS projects, with a source project and a project where files are linked to the files in the source project.
Updated answer for handling both Revit 2015 and Revit 2016 API within the same add-in.
Do you really want to do that?
Well, if you insist, here goes:
Usage:
Untested!
Cheers,
Jeremy
The dynamic in C# allow you to use late-binding. Then I would suggest some reflection to instante the object, like the "logic" below (not tested, needs to be completed)
Note that dynamic is different from var. Your code is using var just as a way to let the compiler decide the type... now dynamic will only define the type on runtime.
i fully agree with maxence' approach above.
i also suggest placing all conditional compilation into one single compatibility module and exporting its functionality as methods or properties, e.g. by defining a method like this:
i implemented this here:
https://github.com/jeremytammik/the_building_coder_samples/blob/master/BuildingCoder/BuildingCoder/Util.cs#L1209-L1225
have fun!
cheers
jeremy
Using the base code from Augusto (upvoted) and a lot more research on Reflection I was able to write this solution: