[Feature] RespondWithModal() which accepts an IModal instance as template (#2564)

* introduce overload for responding to an interaction with an instatiated IModal obj

* add inline docs to ModalInfo.PropertyInfo

* Apply suggestions from code review

Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>

---------

Co-authored-by: Casmir <68127614+csmir@users.noreply.github.com>
This commit is contained in:
Cenk Ergen
2023-02-06 13:52:16 +03:00
committed by GitHub
parent 91e208474d
commit e7bda0f8a5
7 changed files with 79 additions and 1 deletions

View File

@@ -10,7 +10,6 @@ namespace Discord.Interactions
internal static class ReflectionUtils<T>
{
private static readonly TypeInfo ObjectTypeInfo = typeof(object).GetTypeInfo();
internal static T CreateObject (TypeInfo typeInfo, InteractionService commandService, IServiceProvider services = null) =>
CreateBuilder(typeInfo, commandService)(services);
@@ -166,6 +165,21 @@ namespace Discord.Interactions
return Expression.Lambda<Action<T, object>>(assign, instanceParam, valueParam).Compile();
}
internal static Func<T, object> CreateLambdaPropertyGetter(PropertyInfo propertyInfo)
{
var instanceParam = Expression.Parameter(typeof(T), "instance");
var prop = Expression.Property(instanceParam, propertyInfo);
return Expression.Lambda<Func<T, object>>(prop, instanceParam).Compile();
}
internal static Func<T, object> CreateLambdaPropertyGetter(Type type, PropertyInfo propertyInfo)
{
var instanceParam = Expression.Parameter(typeof(T), "instance");
var instanceAccess = Expression.Convert(instanceParam, type);
var prop = Expression.Property(instanceAccess, propertyInfo);
return Expression.Lambda<Func<T, object>>(prop, instanceParam).Compile();
}
internal static Func<object[], object[], T> CreateLambdaMemberInit(TypeInfo typeInfo, ConstructorInfo constructor, Predicate<PropertyInfo> propertySelect = null)
{
propertySelect ??= x => true;