private void Add_StaticFields() { foreach (var i in _relatedUnit.Units) { var unitTypeName = Cfg.TargetUnitTypename; var f1Name = i.FieldName + unitTypeName; var f1Type = unitTypeName; var v = Cfg.Source.Container.GetTypename() + "." + i.FieldName + Cfg.Source.Unit.GetTypename(); v = new CsArguments(v).Create(f1Type); var field = Target.AddField(f1Name, f1Type) .WithStatic() .WithIsReadOnly() .WithVisibility(Visibilities.Internal) .WithConstValue(v) .Description = $"unit 1/{i.UnitShortCode.EffectiveValue}"; var unitDefinitionType = new CsArguments(unitTypeName) .MakeGenericType("UnitDefinition"); var value = new CsArguments(f1Name, Inverse(i.ScaleFactor)).Create(unitDefinitionType); field = Target.AddField(i.FieldName, unitDefinitionType) .WithIsReadOnly() .WithStatic() .WithConstValue(value) .Description = $"unit 1/{i.UnitShortCode.EffectiveValue} with factor"; } }
private void Add_StaticConstructor() { var cw = Ext.Create(GetType()); var array = new CsArguments(_relatedUnit.Units.Select(q => q.FieldName).ToArray()); array.CreateArray(cw, "All = "); var c = Target.AddConstructor().WithStatic().WithBody(cw); Target.AddField("All", AllPropertyTypeName).WithStatic().WithIsReadOnly() .Description = $"All known {Cfg.TargetUnitTypename.FirstLower()} units"; }
private void Add_Properties() { Target.AddField("BaseUnit", Cfg.UnitTypes.Unit.GetTypename()) .WithStatic() .WithIsReadOnly() .WithConstValue(Cfg.BaseUnit.ToString()); Target.AddField("Zero", Cfg.UnitTypes.Value.ValueTypeName) .WithStatic() .WithIsReadOnly() .WithConstValue($"new {Target.Name}(0, BaseUnit)"); }
private void Add_Properties() { foreach (var i in Cfg.Units) { var unitTypeName = Cfg.Name.ToUnitTypeName().GetTypename(); var n2 = i.FieldName + unitTypeName; { var constValue = i.UnitConstructor; if (string.IsNullOrEmpty(constValue)) { var args = i.UnitShortCode.GetCreationArgs(Related); constValue = args.Create(unitTypeName); } Target.AddField(n2, unitTypeName) .WithIsReadOnly() .WithVisibility(Visibilities.Internal) .WithConstValue(constValue) .WithStatic(); } { var args = new[] { n2, i.ScaleFactor }; if (i.Aliases != null) { args = i.Aliases.Plus(args); } var unitDefinitionType = new CsArguments(unitTypeName) .MakeGenericType("UnitDefinition"); // public static readonly UnitDefinition<LengthUnit> Km // = new UnitDefinition<LengthUnit>("km", 1000m); var value = new CsArguments(args).Create(unitDefinitionType); Target.AddField(i.FieldName, unitDefinitionType) .WithIsReadOnly() .WithStatic() .WithConstValue(value); } if (i.AddFromMethod) { var c = Get(Cfg.Name.ValueTypeName, out var file); CsFilesManager.AddGeneratorName(file, GetType().Name); var value = new CsArguments("value", Target.Name + "." + i.FieldName + ".Unit").Create(c.Name); c.AddMethod("From" + i.FieldName, c.Name).WithBody($"return {value};") .WithParameter(new CsMethodParameter("value", "decimal")); } } }
private void Add_Properties() { Target.AddField("BaseUnit", UnitTypeName) .WithStatic() .WithIsReadOnly() .WithConstValue(Cfg.BaseUnitValueSource.ToString()); /*Target.AddField("Zero", Target.Name) * .WithStatic() * .WithIsReadOnly() * .WithConstValue($"new {Target.Name}(0, BaseUnit)");*/ }