Support for creating static variables for a type using attributes
This commit is contained in:
parent
93e256218d
commit
859c410609
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue