среда, 25 октября 2023 г.

Reflection, Find, MethodByName, Call

Reflection

Find Method by its name

https://stackoverflow.com/questions/3110280/how-to-use-reflection-to-call-method-by-name

https://learn.microsoft.com/en-us/dotnet/api/system.delegate.createdelegate?view=net-7.0&redirectedfrom=MSDN#overloads

Type thisType = <your object>.GetType();
MethodInfo theMethod = thisType.GetMethod(<The Method Name>); 
theMethod.Invoke(this, <an object [] of parameters or null>); 

public static R ResponseHelper<T,R>(T request, string serviceAction)
{
    var service = new ContentServiceRef.CMSCoreContentServiceClient();

    var func = (Func<T,R>)Delegate.CreateDelegate(typeof(Func<T,R>),
                                                  service,
                                                  serviceAction);

    return func(request);
}

Комментариев нет:

Отправить комментарий