1
- <#@ template debug="True" language="C#" hostspecific="True" #>
2
- <# // Load types in current assembly. #>
1
+ <# // Load types in current assembly. #>
3
2
<#@ assembly name="$(TargetPath)" #>
4
3
<#@ assembly name="System.Core" #>
5
4
<#@ import namespace="System.Linq" #>
8
7
<#@ import namespace="UnitsNet" #>
9
8
<#@ import namespace="UnitsNet.Attributes" #>
10
9
<#@ import namespace="UnitsNet.Extensions" #>
11
- <#@ include file="Includes\MultipleOutputHelper.ttinclude" once="true"#>
12
- <#@ include file="Includes\Shared.ttinclude" once="true"#>
13
- <# // Disable output of this file, note this causes a build warning #>
14
- <#@ output extension="/" #>
15
- <#//@ output extension=".cs" #>
16
- <#
17
- List<Type> unitAttributeTypes = GetUnitAttributeTypes();
18
- List<string> unitClassNames = GetUnitClassNamesFromUnitAttributeImplementations();
10
+
11
+ <#/* Uncomment below for intellisense/ReSharper support,
12
+ but this will give an error when trying to run __Regenerate.tt template
13
+ since they are included multiple times and once="true" does not
14
+ seem to work as it should. */#>
15
+
16
+ <#//@ include file="MultipleOutputHelper.ttinclude" once="true"#>
17
+ <#//@ include file="Shared.ttinclude" once="true"#><#+
18
+
19
+ private void GenerateUnitConverter(Manager m)
20
+ {
21
+
22
+ List<Type> unitAttributeTypes = GetUnitAttributeTypes();
23
+ List<string> unitClassNames = GetUnitClassNamesFromUnitAttributeImplementations();
19
24
Dictionary<Unit, UnitAttribute> unitToAttribute = GetUnitToAttributeDictionary<UnitAttribute>();
20
25
21
- var m = Manager.Create(Host, GenerationEnvironment);
22
- using (m.StartNewFile("UnitConverter.g.cs"))
26
+ using (m.StartNewFile(@"UnitConverter.g.cs"))
23
27
{
24
28
#>
25
29
// Copyright © 2007 by Initial Force AS. All rights reserved.
@@ -66,10 +70,10 @@ namespace UnitsNet
66
70
return value;
67
71
68
72
double newValue;
69
- <# foreach (string unitName in unitClassNames) #>
70
- <# { #>
71
- if (TryConvertFrom<#=unitName#>(value, fromUnit, toUnit, out newValue)) return newValue;
72
- <# } #>
73
+ <#+ foreach (string unitName in unitClassNames) #>
74
+ <#+ { #>
75
+ if (TryConvertFrom<#=unitName #>(value, fromUnit, toUnit, out newValue)) return newValue;
76
+ <#+ } #>
73
77
74
78
throw new Exception(
75
79
string.Format("Conversion from unit [{0}] to [{1}] is either not valid or not yet implemented.",
@@ -92,75 +96,77 @@ namespace UnitsNet
92
96
return true;
93
97
}
94
98
95
- <# foreach (string unitName in unitClassNames) #>
96
- <# { #>
97
- if (TryConvertFrom<#=unitName#>(value, fromUnit, toUnit, out newValue)) return true;
98
- <# } #>
99
+ <#+ foreach (string unitName in unitClassNames) #>
100
+ <#+ { #>
101
+ if (TryConvertFrom<#=unitName #>(value, fromUnit, toUnit, out newValue)) return true;
102
+ <#+ } #>
99
103
100
104
return false;
101
105
}
102
106
103
107
#region Private
104
108
105
- <# foreach (string unitClassName in unitClassNames) #>
106
- <# { #>
107
- <# List<Unit> unitsOfUnitClass = GetUnitsOfUnitClass(unitClassName, unitAttributeTypes, unitToAttribute); #>
108
- <##>
109
+ <#+ foreach (string unitClassName in unitClassNames) #>
110
+ <#+ { #>
111
+ <#+ List<Unit> unitsOfUnitClass = GetUnitsOfUnitClass(unitClassName, unitAttributeTypes,
112
+ unitToAttribute); #>
113
+
109
114
/// <summary>
110
- /// Try to dynamically convert from <#=unitClassName#> to <paramref name="toUnit"/>.
115
+ /// Try to dynamically convert from <#=unitClassName #> to <paramref name="toUnit"/>.
111
116
/// </summary>
112
117
/// <param name="value">Value to convert from.</param>
113
118
/// <param name="fromUnit">Unit to convert from.</param>
114
119
/// <param name="toUnit">Compatible unit to convert to.</param>
115
120
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
116
121
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
117
- private static bool TryConvertFrom<#=unitClassName#>(double value, Unit fromUnit, Unit toUnit, out double newValue)
122
+ private static bool TryConvertFrom<#=unitClassName #>(double value, Unit fromUnit, Unit toUnit, out double newValue)
118
123
{
119
124
switch (fromUnit)
120
125
{
121
- <#foreach (Unit unit in unitsOfUnitClass)#>
122
- <#{ #>
123
- <# string pluralUnitName = GetUnitPluralName(unitToAttribute, unit);#>
124
- case Unit.<#=unit.ToString()#>:
125
- return TryConvert(<#=unitClassName #>.From<#=pluralUnitName#>(value), toUnit, out newValue);
126
- <# } #>
126
+ <#+ foreach (Unit unit in unitsOfUnitClass) #>
127
+ <#+ { #>
128
+ <#+ string pluralUnitName = GetUnitPluralName(unitToAttribute, unit); #>
129
+ case Unit.<#=unit.ToString() #>:
130
+ return TryConvert(<#=unitClassName #>.From<#=pluralUnitName #>(value), toUnit, out newValue);
131
+ <#+ } #>
127
132
128
133
default:
129
134
newValue = 0;
130
135
return false;
131
136
}
132
137
}
133
- <# }#>
138
+ <#+ } #>
139
+
140
+ <#+ foreach (string unitClassName in unitClassNames) #>
141
+ <#+ { #>
142
+ <#+ List<Unit> unitsOfUnitClass = GetUnitsOfUnitClass(unitClassName, unitAttributeTypes,
143
+ unitToAttribute); #>
134
144
135
- <# foreach (string unitClassName in unitClassNames) #>
136
- <# { #>
137
- <# List<Unit> unitsOfUnitClass = GetUnitsOfUnitClass(unitClassName, unitAttributeTypes, unitToAttribute); #>
138
- <##>
139
145
/// <summary>
140
- /// Try to dynamically convert from <#=unitClassName#> to <paramref name="toUnit"/>.
146
+ /// Try to dynamically convert from <#=unitClassName #> to <paramref name="toUnit"/>.
141
147
/// </summary>
142
148
/// <param name="value">Value to convert from.</param>
143
149
/// <param name="toUnit">Compatible unit to convert to.</param>
144
150
/// <param name="newValue">Value in new unit if successful, zero otherwise.</param>
145
151
/// <returns>True if the two units were compatible and the conversion was successful.</returns>
146
- private static bool TryConvert(<#=unitClassName#> value, Unit toUnit, out double newValue)
152
+ private static bool TryConvert(<#=unitClassName #> value, Unit toUnit, out double newValue)
147
153
{
148
154
switch (toUnit)
149
155
{
150
- <#foreach (Unit unit in unitsOfUnitClass)#>
151
- <#{ #>
152
- case Unit.<#=unit.ToString()#>:
153
- newValue = value.<#=GetUnitPluralName(unitToAttribute, unit)#>;
156
+ <#+ foreach (Unit unit in unitsOfUnitClass) #>
157
+ <#+ { #>
158
+ case Unit.<#=unit.ToString() #>:
159
+ newValue = value.<#=GetUnitPluralName(unitToAttribute, unit) #>;
154
160
return true;
155
- <# } #>
161
+ <#+ } #>
156
162
157
163
default:
158
164
newValue = 0;
159
165
return false;
160
166
}
161
167
}
162
168
163
- <# } #>
169
+ <#+ } #>
164
170
#endregion
165
171
166
172
#region Not implemented as unit class yet, no UnitAttribute for these
@@ -240,11 +246,9 @@ namespace UnitsNet
240
246
#endregion
241
247
}
242
248
}
243
- <#
244
- }
245
-
246
- m.Process(split: true);
247
- #> <#+
248
249
250
+ <#+
251
+ } // using (m.StartNewFile(@"..\UnitConverter.g.cs"))
252
+ } // private void GenerateUnitConverter(Manager m)
249
253
250
254
#>
0 commit comments