Create an 'Util’ class is common practice on Revit development. So what about use .NET Extension API? This is available since .NET 3.0. The syntax is a little different from VB.NET and C#.
Start with a simple idea: convert from feet to meter. For some time I see developers doing it with a regular Utils class. With Extension is basically the same, except that in VB.NET it is required to use a Module (not a Class) and mark all methods with <Extension()> attribute. In C# create a static class with static methods.
VB.NET
Imports System.Runtime.CompilerServices
Public Module Utils
Private Const FEET_TO_METERS As Double = 0.3048
<Extension()> _
Public Function FeetToMeter(feet As Double) As Double
Return feet * FEET_TO_METERS
End Function
<Extension()> _
Public Function MeterToFeet(meter As Double) As Double
Return meter / FEET_TO_METERS
End Function
End Module
C#
public static class Utils
{
private const double FEET_TO_METERS = 0.3048;
public static double FeetToMeter(this double feet)
{
return feet * FEET_TO_METERS;
}
public static double MeterToFeet(this double meter)
{
return meter / FEET_TO_METERS;
}
}
Now the use is even more interesting. Just create any double and call its new method MeterToFeet! The auto-complete feature works just fine.
Now even more fun, try type a (double) number and call its new extension method, works fine too!
And the good news: if we made these changes the code still working on the ‘classic’ way (calling the method by name), so your code should not break because of this change.