Make enums be considered userdata, and not numbers. Allow C# functions that consume enums to use both numbers and userdata

This commit is contained in:
Deukhoofd 2019-01-16 10:50:22 +01:00
parent 0a034013ea
commit 2ef06b3fd7
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
2 changed files with 8 additions and 7 deletions

View File

@ -106,7 +106,6 @@ namespace Upsilon.BoundTypes
throw new Exception("Trying to bind an enum with a type that's not an enum");
Properties = new Dictionary<string, UserDataBoundProperty>();
var enumUnderlyingType = Enum.GetUnderlyingType(enumType);
var enumValues = Enum.GetValues(enumType);
Name = name;
@ -119,11 +118,12 @@ namespace Upsilon.BoundTypes
Properties.Add(valueName, new UserDataBoundProperty()
{
Name = valueName,
ActualType = enumUnderlyingType.ToString(),
Type = Type.Number
ActualType = enumType.ToString(),
Type = Type.UserData
});
}
}
public UserDataBoundEnumDefinition(IEnumerable<string> values, string name) : base(name, new Dictionary<string, UserDataBoundProperty>())
{
Properties = new Dictionary<string, UserDataBoundProperty>();
@ -131,11 +131,11 @@ namespace Upsilon.BoundTypes
foreach (var value in values)
{
var valueName = value.ToString().ToLowerInvariant();
var valueName = value.ToLowerInvariant();
Properties.Add(valueName, new UserDataBoundProperty()
{
Name = valueName,
Type = Type.Number
Type = Type.UserData
});
}
}

View File

@ -141,6 +141,9 @@ namespace Upsilon.StandardLibraries
public static Type DeriveValidTypes(System.Type type)
{
if (type.IsEnum)
return Type.UserData | Type.Number;
var typeCode = System.Type.GetTypeCode(type);
switch (typeCode)
{
@ -181,8 +184,6 @@ namespace Upsilon.StandardLibraries
if (type == typeof(IIterable))
return Type.Table | Type.UserData;
if (type.IsEnum)
return Type.UserData | Type.Number;
if (typeof(IEnumerable).IsAssignableFrom(type))
return Type.Table | Type.UserData;
return Type.UserData;