Skip to content

Commit 3b006c3

Browse files
committed
[增加]1. 增加事件监听的命名判断
1 parent a05ebd3 commit 3b006c3

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

GameFrameX.Core/Hotfix/HotfixModule.cs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,29 +339,44 @@ private bool AddEvent(Type type)
339339
return false;
340340
}
341341

342+
var classFullName = type.FullName;
343+
if (classFullName == null)
344+
{
345+
return false;
346+
}
347+
348+
if (!type.IsSealed)
349+
{
350+
throw new InvalidOperationException($"{classFullName} 必须是标记为sealed的类");
351+
}
352+
353+
if (!classFullName.EndsWith(GlobalConst.EventListenerNameSuffix))
354+
{
355+
throw new Exception($"事件处理器 必须以[{GlobalConst.EventListenerNameSuffix}]结尾,{classFullName}");
356+
}
357+
342358
var compAgentType = type.BaseType.GetGenericArguments()[0];
343359
var compType = compAgentType.BaseType.GetGenericArguments()[0];
344360
var actorType = ComponentRegister.ComponentActorDic[compType];
345361
var evtListenersDic = _actorEvtListeners.GetOrAdd(actorType);
346362

347-
var find = false;
348-
foreach (var attr in type.GetCustomAttributes())
363+
var eventInfoAttributes = type.GetCustomAttributes<EventInfoAttribute>();
364+
var infoAttributes = eventInfoAttributes.ToList();
365+
if (infoAttributes.Count == 0)
349366
{
350-
if (attr is EventInfoAttribute evt)
351-
{
352-
find = true;
353-
354-
var evtId = evt.EventId;
355-
var listeners = evtListenersDic.GetOrAdd(evtId);
356-
listeners.Add((IEventListener)Activator.CreateInstance(type));
357-
}
367+
throw new Exception($"IEventListener:{type.FullName}没有指定监听的事件");
358368
}
359369

360-
if (!find)
370+
var eventInfoAttribute = infoAttributes.FirstOrDefault();
371+
if (eventInfoAttribute == null)
361372
{
362373
throw new Exception($"IEventListener:{type.FullName}没有指定监听的事件");
363374
}
364375

376+
var evtId = eventInfoAttribute.EventId;
377+
var listeners = evtListenersDic.GetOrAdd(evtId);
378+
listeners.Add((IEventListener)Activator.CreateInstance(type));
379+
365380
return true;
366381
}
367382

0 commit comments

Comments
 (0)