File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
Src/UnitsNet/Generated Code/Includes Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 18
18
private static UnitAttribute GetUnitAttributeFromUnitClassName(string unitClassName)
19
19
{
20
20
// Derived UnitAttributes are typically named LengthAttribute, MassAttribute etc.
21
- const string attributeNamespace = "UnitsNet";
21
+ const string attributeNamespace = "UnitsNet.Attributes ";
22
22
string unitAttributeFullName = String.Format("{0}.{1}Attribute", attributeNamespace, unitClassName);
23
23
Type unitAttributeType = typeof (Unit).Assembly.GetType(unitAttributeFullName);
24
24
if (unitAttributeType == null)
25
25
return null;
26
26
27
27
// Example ctor: public AngleAttribute(double ratio, string pluralName = null)
28
- var attr = (UnitAttribute) Activator.CreateInstance(unitAttributeType, new object[] {0.0, null});
28
+ //var attr = (UnitAttribute) Activator.CreateInstance(unitAttributeType, new object[] {0.0, null});
29
+
30
+ ConstructorInfo simplestCtor = unitAttributeType.GetConstructors().OrderBy(ctor => ctor.GetParameters().Length).First();
31
+ object[] parameters = simplestCtor.GetParameters().Select(p => GetDefault(p.ParameterType)).ToArray();
32
+ if (parameters.Length == 0)
33
+ throw new Exception("Ctor with no params found for type: " + unitAttributeType);
34
+
35
+ var attr = (UnitAttribute)simplestCtor.Invoke(parameters);
29
36
return attr;
30
37
}
31
38
39
+ private static object GetDefault(Type type)
40
+ {
41
+ if (type.IsValueType)
42
+ {
43
+ return Activator.CreateInstance(type);
44
+ }
45
+ return null;
46
+ }
47
+
32
48
private Dictionary<Unit, TUnitAttribute> GetUnitToAttributeDictionary<TUnitAttribute>()
33
49
where TUnitAttribute : UnitAttribute
34
50
{
You can’t perform that action at this time.
0 commit comments