2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 13:33:13 +00:00

Make...Template

This commit is contained in:
2022-08-17 22:49:21 +03:00
parent 2f379732ae
commit ebcf8525fa
8 changed files with 353 additions and 268 deletions

View File

@ -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;
}
}