blob: e6110bb6fd1bfeea7557a3bbc397a9e1b2b1b403 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.Collections.Concurrent;
using System.Linq.Expressions;
using System.Reflection;
namespace QtVsTools.Common
{
public class LazyFactory : Concurrent
{
private ConcurrentDictionary<PropertyInfo, object> Objs { get; } = new();
public T Get<T>(Expression<Func<T>> propertyRef, Func<T> initFunc) where T : class
{
if (propertyRef?.Body is not MemberExpression lazyPropertyExpr)
throw new ArgumentException("Expected lambda member expression", "propertyRef");
if (lazyPropertyExpr?.Member is not PropertyInfo lazyProperty)
throw new ArgumentException("Invalid property reference", "propertyRef");
lock (CriticalSection) {
if (!Objs.TryGetValue(lazyProperty, out var lazyObject))
Objs[lazyProperty] = lazyObject = initFunc();
return lazyObject as T;
}
}
}
}
|