Support for creating static variables for a type using attributes

This commit is contained in:
Deukhoofd 2019-02-16 13:54:10 +01:00
parent 93e256218d
commit 859c410609
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 27 additions and 11 deletions

View File

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

View File

@ -2,8 +2,7 @@ using System;
namespace Upsilon.BaseTypes.UserData namespace Upsilon.BaseTypes.UserData
{ {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface, [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface)]
AllowMultiple = false, Inherited = true)]
public class UpsilonUserDataAttribute : Attribute public class UpsilonUserDataAttribute : Attribute
{ {
public UpsilonUserDataAttribute(string name) public UpsilonUserDataAttribute(string name)

View File

@ -16,15 +16,9 @@ namespace Upsilon.BaseTypes.UserData
public static void LoadType(System.Type t, string name) public static void LoadType(System.Type t, string name)
{ {
UserDataBoundTypeDefinition boundType; var boundType = t.IsEnum
if (t.IsEnum) ? new UserDataBoundEnumDefinition(t, name)
{ : UserDataBoundTypeDefinition.Create(t, name);
boundType = new UserDataBoundEnumDefinition(t, name);
}
else
{
boundType = UserDataBoundTypeDefinition.Create(t, name);
}
BoundTypeHandler.LoadUserDataTypeDefinition(boundType); BoundTypeHandler.LoadUserDataTypeDefinition(boundType);
var info = new UserDataType(t); var info = new UserDataType(t);
Types.AddOrUpdate(t, info, (type, dataType) => dataType); Types.AddOrUpdate(t, info, (type, dataType) => dataType);
@ -46,6 +40,14 @@ namespace Upsilon.BaseTypes.UserData
StaticScope.RegisterStaticVariable(attr.Name, def); StaticScope.RegisterStaticVariable(attr.Name, def);
} }
} }
var createStaticAttribute =
(UpsilonCreateStaticAttribute) type.GetCustomAttribute(typeof(UpsilonCreateStaticAttribute));
if (createStaticAttribute != null)
{
var name = createStaticAttribute.Name.ToLowerInvariant();
StaticScope.RegisterStaticVariable(createStaticAttribute.Name, Activator.CreateInstance(type));
}
} }
} }