Lan.cs
1.37 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
namespace HHECS.Web
{
public class Lan
{
private static readonly Type ResourceType = typeof(HHECS.Web.Resources.Shared._Layout);
// 使用Lazy初始化确保线程安全
private static readonly Lazy<Dictionary<string, PropertyInfo>> PropertyCache =
new Lazy<Dictionary<string, PropertyInfo>>(() =>
{
var properties = ResourceType.GetProperties(
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
return properties.ToDictionary(p => p.Name, p => p);
});
public static string Get(string key)
{
var cache = PropertyCache.Value;
if (cache.TryGetValue(key, out var property))
{
return property.GetValue(null) as string ?? $"[{key}]";
}
return $"[{key}]";
}
// 添加一个方法支持默认值
public static string Get(string key, string defaultValue)
{
var cache = PropertyCache.Value;
if (cache.TryGetValue(key, out var property))
{
return property.GetValue(null) as string ?? defaultValue;
}
return defaultValue;
}
}
}