2020-06-02 01:17:26 +03:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2020-02-20 02:30:14 +03:00
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
2020-06-22 19:38:40 +03:00
|
|
|
#if !USE_WINUI3
|
2020-02-20 02:30:14 +03:00
|
|
|
using Windows.UI.Xaml;
|
2020-06-22 19:38:40 +03:00
|
|
|
#else
|
|
|
|
using Microsoft.UI.Xaml;
|
|
|
|
#endif
|
2020-02-20 02:30:14 +03:00
|
|
|
|
|
|
|
namespace Microsoft.ReactNative.Managed
|
|
|
|
{
|
2020-05-07 20:22:40 +03:00
|
|
|
public static class ReactContextGenerator
|
2020-02-20 02:30:14 +03:00
|
|
|
{
|
2020-04-27 19:41:44 +03:00
|
|
|
private static MethodInfo GetMethod(string methodName, Type[] typeArgs, params Type[] requiredArgTypes)
|
2020-02-20 02:30:14 +03:00
|
|
|
{
|
|
|
|
var extMethod =
|
2020-04-27 19:41:44 +03:00
|
|
|
from member in typeof(ReactContext).GetMember(methodName, BindingFlags.Instance | BindingFlags.Public)
|
2020-02-20 02:30:14 +03:00
|
|
|
let method = member as MethodInfo
|
|
|
|
let isGeneric = method.IsGenericMethod
|
|
|
|
where method != null
|
|
|
|
let parameters = method.GetParameters()
|
|
|
|
where parameters.Length == typeArgs.Length + requiredArgTypes.Length
|
|
|
|
&& Enumerable.Range(0, requiredArgTypes.Length).All(i => parameters[i].ParameterType == requiredArgTypes[i])
|
|
|
|
select isGeneric ? method.MakeGenericMethod(typeArgs) : method;
|
|
|
|
return extMethod.First();
|
|
|
|
}
|
|
|
|
|
2020-04-27 19:41:44 +03:00
|
|
|
public static ConstructorInfo ReactContextConstructor() =>
|
|
|
|
typeof(ReactContext).GetConstructor(new Type[] { typeof(IReactContext) });
|
|
|
|
|
2020-02-20 02:30:14 +03:00
|
|
|
public static MethodInfo DispatchEventOf(params Type[] typeArgs)
|
|
|
|
{
|
2020-04-27 19:41:44 +03:00
|
|
|
return GetMethod(nameof(ReactContext.DispatchEvent), typeArgs, typeof(FrameworkElement), typeof(string));
|
2020-02-20 02:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static MethodInfo CallJSFunctionOf(params Type[] typeArgs)
|
|
|
|
{
|
2020-04-27 19:41:44 +03:00
|
|
|
return GetMethod(nameof(ReactContext.CallJSFunction), typeArgs, typeof(string), typeof(string));
|
2020-02-20 02:30:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public static MethodInfo EmitJSEventOf(params Type[] typeArgs)
|
|
|
|
{
|
2020-04-27 19:41:44 +03:00
|
|
|
return GetMethod(nameof(ReactContext.EmitJSEvent), typeArgs, typeof(string), typeof(string));
|
2020-02-20 02:30:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|