Support for easy loading assemblies, and instantly creating enums in the static scope

This commit is contained in:
2018-12-08 11:30:22 +01:00
parent 986d00b1a0
commit f6947194b9
4 changed files with 57 additions and 14 deletions

View File

@@ -0,0 +1,15 @@
using System;
namespace Upsilon.BaseTypes.UserData
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum, AllowMultiple = false, Inherited = true)]
public class UpsilonUserDataAttribute : Attribute
{
public UpsilonUserDataAttribute(string name)
{
Name = name;
}
public string Name { get; }
}
}

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Upsilon.BoundTypes;
using Upsilon.StandardLibraries;
namespace Upsilon.BaseTypes.UserData
{
@@ -8,25 +10,47 @@ namespace Upsilon.BaseTypes.UserData
{
private static readonly Dictionary<System.Type, UserDataType> Types = new Dictionary<System.Type, UserDataType>();
public static void LoadType(System.Type t)
public static void LoadType(System.Type t, string name)
{
var info = new UserDataType(t);
Types.Add(t, info);
UserDataBoundTypeDefinition boundType;
if (t.IsEnum)
{
boundType = new UserDataBoundEnumDefinition(t);
boundType = new UserDataBoundEnumDefinition(t, name);
}
else
{
boundType = UserDataBoundTypeDefinition.Create(t);
boundType = UserDataBoundTypeDefinition.Create(t, name);
}
BoundTypeHandler.LoadUserDataTypeDefinition(boundType);
}
public static void LoadAssembly(Assembly assembly)
{
var types = assembly.GetTypes();
foreach (var type in types)
{
var attr = (UpsilonUserDataAttribute)type.GetCustomAttribute(typeof(UpsilonUserDataAttribute));
if (attr != null)
{
var name = attr.Name.ToLowerInvariant();
LoadType(type, name);
if (type.IsEnum)
{
var def = Activator.CreateInstance(type);
StaticScope.RegisterStaticVariable(attr.Name, def);
}
}
}
}
public static void LoadType<T>()
{
LoadType(typeof(T));
var attr = (UpsilonUserDataAttribute)typeof(T).GetCustomAttribute(typeof(UpsilonUserDataAttribute));
var name = typeof(T).Name;
if (attr != null) name = attr.Name;
LoadType(typeof(T), name);
}
internal static UserDataType GetTypeInfo(System.Type t)