C# Lazy Loaded Automatic Properties

前端 未结 12 2180
执笔经年
执笔经年 2020-12-04 11:08

In C#,

Is there a way to turn an automatic property into a lazy loaded automatic property with a specified default value?

Essentially, I am trying to turn th

12条回答
  •  清歌不尽
    2020-12-04 11:49

    [Serializable]
    public class RaporImza
    {
        private readonly Func _getReportLayout;
        public RaporImza(Func getReportLayout)
        {
            _getReportLayout = getReportLayout;
        }
    
        private ReportConfig _getReportLayoutResult;
        public ReportConfig GetReportLayoutResult => _getReportLayoutResult ?? (_getReportLayoutResult = _getReportLayout());
    
        public string ImzaAtanKisiAdi => GetReportLayoutResult.ReportSignatureName;
    
        public string ImzaAtanKisiUnvani => GetReportLayoutResult.ReportSignatureTitle;
        public byte[] Imza => GetReportLayoutResult.ReportSignature;
    }
    

    and i call like bellow

    result.RaporBilgisi = new ExchangeProgramPersonAllDataModel.RaporImza(() => _reportConfigService.GetReportLayout(documentTypeId));
    

提交回复
热议问题