C# 聚合的自定义名称
在使用不同的聚合器工厂时,存在允许为返回标签定义自定义名称的方法或重写。例如,调用SumAggregatoryFactory对“金额”字段求和将返回一个行标签“金额之和”。如果我们希望它是“总量”、“总量”或其他自定义值呢 创建一个ResultAmount类。这将返回结果的描述,而不是字符串。大概是这样的:C# 聚合的自定义名称,c#,nreco,C#,Nreco,在使用不同的聚合器工厂时,存在允许为返回标签定义自定义名称的方法或重写。例如,调用SumAggregatoryFactory对“金额”字段求和将返回一个行标签“金额之和”。如果我们希望它是“总量”、“总量”或其他自定义值呢 创建一个ResultAmount类。这将返回结果的描述,而不是字符串。大概是这样的: class ResultAmount { public string Label { get; set; }
class ResultAmount
{
public string Label { get; set; }
public decimal SumAmount { get; set; }
public override string ToString()
{
return $"{Label}: {SumAmount}";
}
}
对于不同的工厂,标签值可能不同。
然后你有一个工厂:
class TotalCalculations
{
public ResultAmount SumAggregatoryFactory()
{
return new ResultAmount
{
Label = "Total",
SumAmount = 100
};
}
}
以及呼叫工厂的要点:
class BillingService
{
public void Print(TotalCalculations calc)
{
//when calling the method, you can use the standard Label
string original = calc.SumAggregatoryFactory().ToString();
//or take only the sum and configure the result string yourself
string custom = $"My message: {calc.SumAggregatoryFactory().SumAmount}";
}
}
创建一个ResultAmount类。这将返回结果的描述,而不是字符串。大概是这样的:
class ResultAmount
{
public string Label { get; set; }
public decimal SumAmount { get; set; }
public override string ToString()
{
return $"{Label}: {SumAmount}";
}
}
对于不同的工厂,标签值可能不同。
然后你有一个工厂:
class TotalCalculations
{
public ResultAmount SumAggregatoryFactory()
{
return new ResultAmount
{
Label = "Total",
SumAmount = 100
};
}
}
以及呼叫工厂的要点:
class BillingService
{
public void Print(TotalCalculations calc)
{
//when calling the method, you can use the standard Label
string original = calc.SumAggregatoryFactory().ToString();
//or take only the sum and configure the result string yourself
string custom = $"My message: {calc.SumAggregatoryFactory().SumAmount}";
}
}
如果使用数据透视工具包编写器(如
数据透视表HtmlWriter
),则可以使用FormatMeasureHeader属性自定义度量值标签:
pvtHtmlWr.FormatMeasureHeader = (aggrFactory, measureIndex) => {
// customize header by factory type or measure index
return aggrFactory.ToString();
};
(另见:)
在自定义呈现的情况下,您可以根据聚合器类型定义自己的度量值名称格式。如果使用数据透视工具包编写器(如
PivotTableHtmlWriter
),您可以使用FormatMeasureHeader属性自定义度量值标签:
pvtHtmlWr.FormatMeasureHeader = (aggrFactory, measureIndex) => {
// customize header by factory type or measure index
return aggrFactory.ToString();
};
(另见:)
在自定义呈现的情况下,您可以根据聚合器类型定义自己的度量值名称格式