Просмотр исходного кода

防止Model层没有实现任何事件时,字典types因缺少EventAttribute key而报空指针错误。 (#155)

ks 6 лет назад
Родитель
Сommit
b2f6aca427
1 измененных файлов с 13 добавлено и 10 удалено
  1. 13 10
      Unity/Assets/Model/Base/Object/EventSystem.cs

+ 13 - 10
Unity/Assets/Model/Base/Object/EventSystem.cs

@@ -116,20 +116,23 @@ namespace ETModel
 			}
 
 			this.allEvents.Clear();
-			foreach (Type type in types[typeof(EventAttribute)])
+			if (this.types.ContainsKey(typeof (EventAttribute)))
 			{
-				object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
-
-				foreach (object attr in attrs)
+				foreach (Type type in types[typeof(EventAttribute)])
 				{
-					EventAttribute aEventAttribute = (EventAttribute)attr;
-					object obj = Activator.CreateInstance(type);
-					IEvent iEvent = obj as IEvent;
-					if (iEvent == null)
+					object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
+
+					foreach (object attr in attrs)
 					{
-						Log.Error($"{obj.GetType().Name} 没有继承IEvent");
+						EventAttribute aEventAttribute = (EventAttribute)attr;
+						object obj = Activator.CreateInstance(type);
+						IEvent iEvent = obj as IEvent;
+						if (iEvent == null)
+						{
+							Log.Error($"{obj.GetType().Name} 没有继承IEvent");
+						}
+						this.RegisterEvent(aEventAttribute.Type, iEvent);
 					}
-					this.RegisterEvent(aEventAttribute.Type, iEvent);
 				}
 			}