Support for easy loading assemblies, and instantly creating enums in the static scope
This commit is contained in:
15
Upsilon/BaseTypes/UserData/UpsilonUserDataAttribute.cs
Normal file
15
Upsilon/BaseTypes/UserData/UpsilonUserDataAttribute.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user