diff --git a/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs b/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs index 377d6730..37522633 100644 --- a/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs +++ b/src/Discord.Net.Interactions/Extensions/IDiscordInteractionExtensions.cs @@ -68,8 +68,13 @@ namespace Discord.Interactions { case TextInputComponentInfo textComponent: { + var boxedValue = textComponent.Getter(modal); + var value = textComponent.TypeOverridesToString + ? boxedValue.ToString() + : boxedValue as string; + builder.AddTextInput(textComponent.Label, textComponent.CustomId, textComponent.Style, textComponent.Placeholder, textComponent.IsRequired ? textComponent.MinLength : null, - textComponent.MaxLength, textComponent.IsRequired, textComponent.Getter(modal) as string); + textComponent.MaxLength, textComponent.IsRequired, value); } break; default: diff --git a/src/Discord.Net.Interactions/Info/InputComponents/TextInputComponentInfo.cs b/src/Discord.Net.Interactions/Info/InputComponents/TextInputComponentInfo.cs index 613549fe..6831c795 100644 --- a/src/Discord.Net.Interactions/Info/InputComponents/TextInputComponentInfo.cs +++ b/src/Discord.Net.Interactions/Info/InputComponents/TextInputComponentInfo.cs @@ -1,3 +1,5 @@ +using System; + namespace Discord.Interactions { /// @@ -5,6 +7,12 @@ namespace Discord.Interactions /// public class TextInputComponentInfo : InputComponentInfo { + /// + /// true when overrides . + /// + internal bool TypeOverridesToString => _typeOverridesToString.Value; + private readonly Lazy _typeOverridesToString; + /// /// Gets the style of the text input. /// @@ -37,6 +45,8 @@ namespace Discord.Interactions MinLength = builder.MinLength; MaxLength = builder.MaxLength; InitialValue = builder.InitialValue; + + _typeOverridesToString = new(() => ReflectionUtils.OverridesToString(Type)); } } } diff --git a/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs b/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs index b401027f..fe4c4f0b 100644 --- a/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs +++ b/src/Discord.Net.Interactions/Utilities/ReflectionUtils.cs @@ -177,7 +177,8 @@ namespace Discord.Interactions { var instanceParam = Expression.Parameter(typeof(T), "instance"); var prop = Expression.Property(instanceParam, propertyInfo); - return Expression.Lambda>(prop, instanceParam).Compile(); + var cast = Expression.Convert(prop, typeof(object)); + return Expression.Lambda>(cast, instanceParam).Compile(); } internal static Func CreateLambdaPropertyGetter(Type type, PropertyInfo propertyInfo) @@ -185,7 +186,8 @@ namespace Discord.Interactions var instanceParam = Expression.Parameter(typeof(T), "instance"); var instanceAccess = Expression.Convert(instanceParam, type); var prop = Expression.Property(instanceAccess, propertyInfo); - return Expression.Lambda>(prop, instanceParam).Compile(); + var cast = Expression.Convert(prop, typeof(object)); + return Expression.Lambda>(cast, instanceParam).Compile(); } internal static Func CreateLambdaMemberInit(TypeInfo typeInfo, ConstructorInfo constructor, Predicate propertySelect = null) @@ -227,5 +229,29 @@ namespace Discord.Interactions return instance; }; } + + /// + /// Checks whether or any base type of it overrides . + /// + /// The type to check. If null will be used. + internal static bool OverridesToString(Type type = null) + { + type ??= typeof(T); + + do + { +#if NET6_0_OR_GREATER + var method = type.GetMethod(nameof(ToString), bindingAttr: BindingFlags.Public | BindingFlags.Instance, types: Type.EmptyTypes); +#else + var method = type.GetMethods(BindingFlags.Public | BindingFlags.Instance) + .SingleOrDefault(m => m.Name == nameof(ToString) && m.GetParameters().Length == 0); +#endif + if (method != null) + return true; + } + while ((type = type.BaseType) != typeof(object)); + + return false; + } } }