Skip to content

Commit c1253ca

Browse files
committed
feat(本地化): 为资源提供者添加AssemblyName属性防止重复注册
在IResourceProvider接口中添加AssemblyName属性用于标识资源提供者,并在ResourceManager中检查重复注册 同时更新测试类实现该属性
1 parent 048fa3d commit c1253ca

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

GameFrameX.Foundation.Localization/Core/IResourceProvider.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ namespace GameFrameX.Foundation.Localization.Core;
1515
/// </remarks>
1616
public interface IResourceProvider
1717
{
18+
/// <summary>
19+
/// 获取资源提供者的名称
20+
/// </summary>
21+
/// <value>
22+
/// 资源提供者的名称,用于标识资源提供者
23+
/// </value>
24+
string AssemblyName { get; }
25+
1826
/// <summary>
1927
/// 获取本地化字符串
2028
/// </summary>

GameFrameX.Foundation.Localization/Core/ResourceManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ private void LoadProviders()
150150
var assemblyProviders = _assemblyProviders.Value;
151151
foreach (var provider in assemblyProviders)
152152
{
153+
if (_providers.Exists(m => m.AssemblyName == provider.Value.AssemblyName))
154+
{
155+
continue;
156+
}
157+
153158
_providers.Insert(0, provider.Value); // 插入到列表开头,优先级更高
154159
}
155160
}

GameFrameX.Foundation.Tests/Localization/LocalizationServiceTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ public TestResourceProvider(string value)
271271
_value = value;
272272
}
273273

274+
/// <summary>
275+
/// 获取资源提供者的名称
276+
/// </summary>
277+
/// <value>
278+
/// 资源提供者的名称,用于标识资源提供者
279+
/// </value>
280+
public string AssemblyName => GetType().Assembly.FullName;
281+
274282
public string GetString(string key)
275283
{
276284
return key == "Test.Key" ? _value : key;

GameFrameX.Foundation.Tests/Localization/ResourceManagerTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ private class TestResourceProvider : IResourceProvider
227227
{
228228
private readonly string _value;
229229

230+
/// <summary>
231+
/// 获取资源提供者的名称
232+
/// </summary>
233+
/// <value>
234+
/// 资源提供者的名称,用于标识资源提供者
235+
/// </value>
236+
public string AssemblyName => GetType().Assembly.FullName;
237+
230238
public TestResourceProvider(string value)
231239
{
232240
_value = value;
@@ -243,6 +251,14 @@ public string GetString(string key)
243251
/// </summary>
244252
private class FaultyResourceProvider : IResourceProvider
245253
{
254+
/// <summary>
255+
/// 获取资源提供者的名称
256+
/// </summary>
257+
/// <value>
258+
/// 资源提供者的名称,用于标识资源提供者
259+
/// </value>
260+
public string AssemblyName => GetType().Assembly.FullName;
261+
246262
public string GetString(string key)
247263
{
248264
throw new InvalidOperationException("Test exception");

0 commit comments

Comments
 (0)