This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
/
EntryPointExecutor.cs
122 lines (101 loc) · 3.8 KB
/
EntryPointExecutor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using Microsoft.Dnx.Runtime.Common.DependencyInjection;
namespace Microsoft.Dnx.Runtime.Common
{
internal static class EntryPointExecutor
{
public static Task<int> Execute(Assembly assembly, string[] args, IServiceProvider serviceProvider)
{
object instance;
MethodInfo entryPoint;
if (!TryGetEntryPoint(assembly, serviceProvider, out instance, out entryPoint))
{
return Task.FromResult(1);
}
object result = null;
var parameters = entryPoint.GetParameters();
try
{
if (parameters.Length == 0)
{
result = entryPoint.Invoke(instance, null);
}
else if (parameters.Length == 1)
{
result = entryPoint.Invoke(instance, new object[] { args });
}
}
catch (Exception ex)
{
if (ex is TargetInvocationException)
{
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
}
throw;
}
if (result is int)
{
return Task.FromResult((int)result);
}
if (result is Task<int>)
{
return (Task<int>)result;
}
if (result is Task)
{
return ((Task)result).ContinueWith(t =>
{
return 0;
});
}
return Task.FromResult(0);
}
private static bool TryGetEntryPoint(Assembly assembly, IServiceProvider serviceProvider, out object instance, out MethodInfo entryPoint)
{
string name = assembly.GetName().Name;
instance = null;
// Add support for console apps
// This allows us to boot any existing console application
// under the runtime
entryPoint = GetEntryPoint(assembly);
if (entryPoint != null)
{
return true;
}
var programType = assembly.GetType("Program") ?? assembly.GetType(name + ".Program");
if (programType == null)
{
var programTypeInfo = assembly.DefinedTypes.FirstOrDefault(t => t.Name == "Program");
if (programTypeInfo == null)
{
System.Console.WriteLine("'{0}' does not contain a static 'Main' method suitable for an entry point", name);
return false;
}
programType = programTypeInfo.AsType();
}
entryPoint = programType.GetTypeInfo().GetDeclaredMethods("Main").FirstOrDefault();
if (entryPoint == null)
{
System.Console.WriteLine("'{0}' does not contain a 'Main' method suitable for an entry point", name);
return false;
}
instance = programType.GetTypeInfo().IsAbstract ? null : ActivatorUtilities.CreateInstance(serviceProvider, programType);
return true;
}
private static MethodInfo GetEntryPoint(Assembly assembly)
{
#if DNX451
return assembly.EntryPoint;
#else
// Temporary until https://github.com/dotnet/corefx/issues/3336 is fully merged and built
return assembly.GetType().GetRuntimeProperty("EntryPoint").GetValue(assembly) as MethodInfo;
#endif
}
}
}