forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionOperationExecutor.cs
More file actions
93 lines (80 loc) · 3.68 KB
/
Copy pathReflectionOperationExecutor.cs
File metadata and controls
93 lines (80 loc) · 3.68 KB
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
// 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Internal;
namespace Microsoft.EntityFrameworkCore.Tools
{
internal class ReflectionOperationExecutor : OperationExecutorBase
{
private readonly object _executor;
private readonly Assembly _commandsAssembly;
private const string ReportHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationReportHandler";
private const string ResultHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationResultHandler";
private readonly Type _resultHandlerType;
public ReflectionOperationExecutor(
string assembly,
string startupAssembly,
string projectDir,
string dataDirectory,
string rootNamespace,
string language)
: base(assembly, startupAssembly, projectDir, dataDirectory, rootNamespace, language)
{
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
_commandsAssembly = Assembly.Load(new AssemblyName { Name = DesignAssemblyName });
var reportHandlerType = _commandsAssembly.GetType(ReportHandlerTypeName, throwOnError: true, ignoreCase: false);
var reportHandler = Activator.CreateInstance(
reportHandlerType,
(Action<string>)Reporter.WriteError,
(Action<string>)Reporter.WriteWarning,
(Action<string>)Reporter.WriteInformation,
(Action<string>)Reporter.WriteVerbose);
_executor = Activator.CreateInstance(
_commandsAssembly.GetType(ExecutorTypeName, throwOnError: true, ignoreCase: false),
reportHandler,
new Dictionary<string, string>
{
{ "targetName", AssemblyFileName },
{ "startupTargetName", StartupAssemblyFileName },
{ "projectDir", ProjectDirectory },
{ "rootNamespace", RootNamespace },
{ "language", Language },
{ "toolsVersion", ProductInfo.GetVersion() }
});
_resultHandlerType = _commandsAssembly.GetType(ResultHandlerTypeName, throwOnError: true, ignoreCase: false);
}
protected override object CreateResultHandler()
=> Activator.CreateInstance(_resultHandlerType);
protected override void Execute(string operationName, object resultHandler, IDictionary arguments)
=> Activator.CreateInstance(
_commandsAssembly.GetType(ExecutorTypeName + "+" + operationName, throwOnError: true, ignoreCase: true),
_executor,
resultHandler,
arguments);
private Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
foreach (var extension in new[] { ".dll", ".exe" })
{
var path = Path.Combine(AppBasePath, assemblyName.Name + extension);
if (File.Exists(path))
{
try
{
return Assembly.LoadFrom(path);
}
catch
{
}
}
}
return null;
}
public override void Dispose()
=> AppDomain.CurrentDomain.AssemblyResolve -= ResolveAssembly;
}
}