我想通過刪除/替換後臺字段來找到PostSharp是否可以很容易地用於更改類的結構。PostSharp:刪除/替換後臺字段
下面是示例:
讓我們說,我有以下實體
class DataItem
{
public Int32 Id { get; set; }
public String Name { get; set; }
public Decimal? HourlyRateMon { get; set; }
public Decimal? HourlyRateTue { get; set; }
public Decimal? HourlyRateWed { get; set; }
public Decimal? HourlyRateThu { get; set; }
public Decimal? HourlyRateFri { get; set; }
}
將使用4 + 8 + 5 * 20 + 16個字節的開銷上每64位= 128個字節實例。
我想什麼做的是應用方面,將通過打包可空小數減少對象的內存佔用 - 像
internal class DataItem
{
private CompactDecimal _hourlyRateFri;
private CompactDecimal _hourlyRateMon;
private CompactDecimal _hourlyRateThu;
private CompactDecimal _hourlyRateTue;
private CompactDecimal _hourlyRateWed;
public int Id { get; set; }
public string Name { get; set; }
public decimal? HourlyRateMon
{
get { return _hourlyRateMon; }
set { _hourlyRateMon = value; }
}
public decimal? HourlyRateTue
{
get { return _hourlyRateTue; }
set { _hourlyRateTue = value; }
}
public decimal? HourlyRateWed
{
get { return _hourlyRateWed; }
set { _hourlyRateWed = value; }
}
public decimal? HourlyRateThu
{
get { return _hourlyRateThu; }
set { _hourlyRateThu = value; }
}
public decimal? HourlyRateFri
{
get { return _hourlyRateFri; }
set { _hourlyRateFri = value; }
}
}
internal struct CompactDecimal
{
public uint Offset;
public static implicit operator decimal?(CompactDecimal d)
{
// here I can, for instance, go to a shared stream at the stream offset and read the decimal
}
public static implicit operator CompactDecimal(decimal? d)
{
// here I can, for instance, serialize the decimal to a shared stream and get the stream offset
}
}
將使用4個+ 8個+ 5個* 4個+ 16個字節64位的開銷=每個實例48字節,節省80字節/實例。如果我們需要存儲數百萬個實例,它會爲我們節省大量內存。
我試着創建一個LocationInterceptionAspect派生的屬性,但我沒有得到預期的結果 - 不僅原始支持字段仍然存在,但我也有10個TestAttribute實例(5個屬性,每個獲取/設置):
// Fields
[NonSerialized]
private TestAttribute <>z__aspect10;
[NonSerialized]
private TestAttribute <>z__aspect11;
[NonSerialized]
private TestAttribute <>z__aspect12;
[NonSerialized]
private TestAttribute <>z__aspect13;
[NonSerialized]
private TestAttribute <>z__aspect4;
[NonSerialized]
private TestAttribute <>z__aspect5;
[NonSerialized]
private TestAttribute <>z__aspect6;
[NonSerialized]
private TestAttribute <>z__aspect7;
[NonSerialized]
private TestAttribute <>z__aspect8;
[NonSerialized]
private TestAttribute <>z__aspect9;
private decimal? <HourlyRateFri>k__OriginalField;
private decimal? <HourlyRateMon>k__OriginalField;
private decimal? <HourlyRateThu>k__OriginalField;
private decimal? <HourlyRateTue>k__OriginalField;
private decimal? <HourlyRateWed>k__OriginalField;
要麼我做錯了什麼或PostSharp不支持我想實現的。
的TestAttribute不現在做任何事情 -
[Serializable]
public class TestAttribute : LocationInterceptionAspect, IInstanceScopedAspect
{
public override void CompileTimeInitialize(LocationInfo targetLocation, AspectInfo aspectInfo)
{
//base.CompileTimeInitialize(targetLocation, aspectInfo);
}
#region <<< Public methods >>>
public Object CreateInstance(AdviceArgs adviceArgs)
{
return MemberwiseClone();
}
public override void OnGetValue(LocationInterceptionArgs args)
{
//args.
//base.OnGetValue(args);
}
public override void OnSetValue(LocationInterceptionArgs args)
{
//base.OnSetValue(args);
}
public void RuntimeInitializeInstance()
{
}
#endregion
}
任何幫助表示讚賞。