mirror of
				https://github.com/esiur/esiur-dotnet.git
				synced 2025-10-31 07:51:36 +00:00 
			
		
		
		
	Make...Template
This commit is contained in:
		| @@ -3,6 +3,7 @@ using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Reflection; | ||||
| using System.Runtime.CompilerServices; | ||||
| using System.Text; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| @@ -60,4 +61,51 @@ public class EventTemplate : MemberTemplate | ||||
|         this.Listenable = listenable; | ||||
|         this.ArgumentType = argumentType; | ||||
|     } | ||||
|  | ||||
|     public static EventTemplate MakeEventTemplate(Type type, EventInfo ei, byte index = 0, string customName = null, TypeTemplate typeTemplate = null) | ||||
|     { | ||||
|         var argType = ei.EventHandlerType.GenericTypeArguments[0]; | ||||
|         var evtType = RepresentationType.FromType(argType); | ||||
|  | ||||
|         if (evtType == null) | ||||
|             throw new Exception($"Unsupported type `{argType}` in event `{type.Name}.{ei.Name}`"); | ||||
|  | ||||
|         var annotationAttr = ei.GetCustomAttribute<AnnotationAttribute>(true); | ||||
|         var listenableAttr = ei.GetCustomAttribute<ListenableAttribute>(true); | ||||
|         var nullableAttr = ei.GetCustomAttribute<NullableAttribute>(true); | ||||
|         var nullableContextAttr = ei.GetCustomAttribute<NullableContextAttribute>(true); | ||||
|  | ||||
|         var flags = nullableAttr?.Flags?.ToList() ?? new List<byte>(); | ||||
|  | ||||
|         // skip the eventHandler class | ||||
|         if (flags.Count > 1) | ||||
|             flags = flags.Skip(1).ToList(); | ||||
|  | ||||
|         if (nullableContextAttr?.Flag == 2) | ||||
|         { | ||||
|             if (flags.Count == 1) | ||||
|                 evtType.SetNotNull(flags.FirstOrDefault()); | ||||
|             else | ||||
|                 evtType.SetNotNull(flags); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             if (flags.Count == 1) | ||||
|                 evtType.SetNull(flags.FirstOrDefault()); | ||||
|             else | ||||
|                 evtType.SetNull(flags); | ||||
|         } | ||||
|  | ||||
|         var et = new EventTemplate(typeTemplate, index, customName ?? ei.Name, ei.DeclaringType != type, evtType); | ||||
|         et.EventInfo = ei; | ||||
|  | ||||
|         if (annotationAttr != null) | ||||
|             et.Annotation = annotationAttr.Annotation; | ||||
|  | ||||
|         if (listenableAttr != null) | ||||
|             et.Listenable = true; | ||||
|  | ||||
|         return et; | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user