Upsilon/Upsilon/BoundTypes/UserDataBoundTypeDefinition.cs

101 lines
3.3 KiB
C#
Raw Normal View History

2018-12-05 14:14:31 +00:00
using System;
2018-11-29 17:09:08 +00:00
using System.Collections.Generic;
2018-12-03 17:32:27 +00:00
using System.Linq;
2018-11-29 17:09:08 +00:00
using Upsilon.BaseTypes;
2018-12-05 14:14:31 +00:00
using Type = Upsilon.BaseTypes.Type;
2018-11-29 17:09:08 +00:00
namespace Upsilon.BoundTypes
{
public class UserDataBoundTypeDefinition : BoundTypeDefinition
{
public string Name { get; }
2018-12-05 14:14:31 +00:00
public Dictionary<string, UserDataBoundProperty> Properties { get; protected set; }
2018-11-29 17:09:08 +00:00
2018-12-05 14:14:31 +00:00
internal UserDataBoundTypeDefinition(System.Type backingType)
2018-11-29 17:09:08 +00:00
: base(Type.UserData, backingType)
{
Name = backingType.Name;
2018-12-05 14:14:31 +00:00
}
public static UserDataBoundTypeDefinition Create(System.Type backingType)
{
var obj = new UserDataBoundTypeDefinition(backingType)
{
Properties = new Dictionary<string, UserDataBoundProperty>()
};
2018-12-03 17:32:27 +00:00
var fields = backingType.GetFields().Select(x => new UserDataBoundProperty()
{
2018-12-05 14:14:31 +00:00
Name = x.Name,
2018-12-03 17:32:27 +00:00
ActualType = x.FieldType.Name,
2018-12-05 14:14:31 +00:00
Type = x.FieldType.GetScriptType(),
2018-12-03 17:32:27 +00:00
});
foreach (var f in fields)
{
2018-12-05 14:14:31 +00:00
obj.Properties.Add(f.Name.ToLowerInvariant(), f);
2018-12-03 17:32:27 +00:00
}
var properties = backingType.GetProperties().Select(x => new UserDataBoundProperty()
{
Name = x.Name,
ActualType = x.PropertyType.Name,
Type = x.PropertyType.GetScriptType(),
});
foreach (var f in properties)
{
2018-12-05 14:14:31 +00:00
obj.Properties.Add(f.Name.ToLowerInvariant(), f);
2018-12-03 17:32:27 +00:00
}
var methods = backingType.GetMethods().Select(x => new UserDataBoundMethod()
{
Name = x.Name,
Type = Type.Function,
ResultType = x.ReturnType.GetScriptType()
});
foreach (var f in methods)
{
2018-12-05 14:14:31 +00:00
obj.Properties.Add(f.Name.ToLowerInvariant(), f);
2018-12-03 17:32:27 +00:00
}
2018-12-05 14:14:31 +00:00
return obj;
2018-11-29 17:09:08 +00:00
}
2018-12-05 14:14:31 +00:00
}
2018-11-29 17:09:08 +00:00
2018-12-05 14:14:31 +00:00
public class UserDataBoundEnumDefinition : UserDataBoundTypeDefinition
{
public UserDataBoundEnumDefinition(System.Type enumType) : base(enumType)
2018-11-29 17:09:08 +00:00
{
2018-12-05 14:14:31 +00:00
if (!enumType.IsEnum)
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);
for (var i=0; i < enumValues.Length; i++)
{
var value = enumValues.GetValue(i);
var name = value.ToString().ToLowerInvariant();
Properties.Add(name, new UserDataBoundProperty()
{
Name = name,
ActualType = enumUnderlyingType.ToString(),
Type = Type.Number
});
}
2018-11-29 17:09:08 +00:00
}
}
public class UserDataBoundProperty
{
public string Name { get; set; }
public Type Type { get; set; }
2018-12-03 17:32:27 +00:00
public virtual string ActualType { get; set; }
2018-11-29 17:09:08 +00:00
public string Comment { get; set; }
}
2018-12-03 17:32:27 +00:00
public class UserDataBoundMethod: UserDataBoundProperty
{
public override string ActualType => "Function";
public Type ResultType { get; set; }
}
2018-11-29 17:09:08 +00:00
}