1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Improved boxed value support in attribute data

This commit is contained in:
Brian Fiete 2023-10-10 10:36:04 -07:00
parent 2dcc9552e2
commit a34e5a737d
10 changed files with 222 additions and 9 deletions

View file

@ -1,3 +1,5 @@
#pragma warning disable 168
using System;
namespace Tests

View file

@ -213,6 +213,43 @@ namespace Tests
}
}
[AttributeUsage(.Field, .ReflectAttribute)]
struct OptionAttribute : Attribute, IOnFieldInit
{
public Object mDefaultValue;
public String mShortName;
public String mLongName;
public this(Object defaultValue, String shortName, String longName)
{
mDefaultValue = defaultValue;
mShortName = shortName;
mLongName = longName;
}
public void OnFieldInit(FieldInfo fieldInfo, Self* prev)
{
if (mDefaultValue != null)
{
Type defaultType = mDefaultValue.GetType();
if (defaultType.IsBoxed)
defaultType = defaultType.UnderlyingType;
if ((defaultType == fieldInfo.FieldType) || (defaultType.UnderlyingType == fieldInfo.FieldType))
return;
Runtime.FatalError(scope $"Default value type mismatch. Expected {fieldInfo.FieldType.GetFullName(.. scope .())} but got {defaultType.GetFullName(.. scope .())}");
}
}
}
class ClassF
{
[Option("123", "Short", "Long")]
String mStr;
[Option((int32)123, "Short", "Long")]
int32 mInt;
}
[Test]
static void TestTypes()
{
@ -372,6 +409,30 @@ namespace Tests
methodIdx++;
}
for (var fieldInfo in typeof(ClassF).GetFields())
{
int idx = @fieldInfo.Index;
var oa = fieldInfo.GetCustomAttribute<OptionAttribute>().Value;
void TestOA()
{
switch (idx)
{
case 0:
Test.Assert(oa.mDefaultValue == "123");
case 1:
Test.Assert(oa.mDefaultValue == (int32)123);
}
}
TestOA();
for (var option in fieldInfo.GetCustomAttributes())
{
oa = option.Get<OptionAttribute>();
TestOA();
}
}
}
[Test]