This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Handlers,iOS,Android] Add AppLoader
- Loading branch information
Showing
11 changed files
with
131 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Platform.Handlers/src/Xamarin.Platform.Handlers/Hosting/IStartup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Xamarin.Platform.Hosting | ||
{ | ||
public interface IStartup | ||
{ | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Platform.Handlers/src/Xamarin.Platform.Handlers/Hosting/StartupLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Xamarin.Platform.Core; | ||
|
||
namespace Xamarin.Platform.Hosting | ||
{ | ||
internal static class AppLoader | ||
{ | ||
public static void ConfigureAppServices<TApplication>(HostBuilderContext context, IServiceCollection services, object app) where TApplication : class, IApp | ||
{ | ||
services.AddSingleton<IApp, TApplication>((serviceProvider) => (TApplication)app); | ||
|
||
var startupType = typeof(TApplication); | ||
|
||
var environmentName = context.HostingEnvironment.EnvironmentName; | ||
|
||
var servicesMethod = FindMethod(startupType, "Configure{0}Services", environmentName, typeof(IServiceProvider), required: false) | ||
?? FindMethod(startupType, "Configure{0}Services", environmentName, typeof(void), required: false); | ||
|
||
if (servicesMethod != null) | ||
servicesMethod.Invoke(app, new object[2] { context, services }); | ||
} | ||
|
||
static MethodInfo? FindMethod(Type startupType, string methodName, string environmentName, Type? returnType = null, bool required = true) | ||
{ | ||
var methodNameWithEnv = string.Format(CultureInfo.InvariantCulture, methodName, environmentName); | ||
var methodNameWithNoEnv = string.Format(CultureInfo.InvariantCulture, methodName, ""); | ||
|
||
var methods = startupType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); | ||
var selectedMethods = methods.Where(method => method.Name.Equals(methodNameWithEnv, StringComparison.OrdinalIgnoreCase)).ToList(); | ||
if (selectedMethods.Count > 1) | ||
{ | ||
throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", methodNameWithEnv)); | ||
} | ||
if (selectedMethods.Count == 0) | ||
{ | ||
selectedMethods = methods.Where(method => method.Name.Equals(methodNameWithNoEnv, StringComparison.OrdinalIgnoreCase)).ToList(); | ||
if (selectedMethods.Count > 1) | ||
{ | ||
throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", methodNameWithNoEnv)); | ||
} | ||
} | ||
|
||
var methodInfo = selectedMethods.FirstOrDefault(); | ||
if (methodInfo == null) | ||
{ | ||
if (required) | ||
{ | ||
throw new InvalidOperationException(string.Format("A public method named '{0}' or '{1}' could not be found in the '{2}' type.", | ||
methodNameWithEnv, | ||
methodNameWithNoEnv, | ||
startupType.FullName)); | ||
|
||
} | ||
return null; | ||
} | ||
if (returnType != null && methodInfo.ReturnType != returnType) | ||
{ | ||
if (required) | ||
{ | ||
throw new InvalidOperationException(string.Format("The '{0}' method in the type '{1}' must have a return type of '{2}'.", | ||
methodInfo.Name, | ||
startupType.FullName, | ||
returnType.Name)); | ||
} | ||
return null; | ||
} | ||
return methodInfo; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters