Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Commit 14e8e85

Browse files
committed
Add support for a netcore50 target
1 parent 19f7250 commit 14e8e85

11 files changed

+306
-37
lines changed
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#if PROXY_SUPPORT
5+
46
using System;
57
using System.Reflection;
68
using System.Reflection.Emit;
@@ -17,12 +19,8 @@ public static class ProxyAssembly
1719
static ProxyAssembly()
1820
{
1921
var assemblyName = new AssemblyName("Microsoft.Framework.Notification.ProxyAssembly");
20-
21-
#if NET45 || DNX451
22-
var access = AssemblyBuilderAccess.RunAndSave;
23-
#else
2422
var access = AssemblyBuilderAccess.Run;
25-
#endif
23+
2624
AssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, access);
2725
ModuleBuilder = AssemblyBuilder.DefineDynamicModule("Microsoft.Framework.Notification.ProxyAssembly.dll");
2826
}
@@ -36,15 +34,6 @@ public static TypeBuilder DefineType(
3634
name = name + "_" + Counter++;
3735
return ModuleBuilder.DefineType(name, attributes, baseType, interfaces);
3836
}
39-
40-
#if NET45 || DNX451
41-
42-
public static void WriteToFile(string file)
43-
{
44-
AssemblyBuilder.Save(file);
45-
}
46-
47-
#endif
48-
4937
}
50-
}
38+
}
39+
#endif

src/Microsoft.Framework.Notification/Internal/ProxyMethodEmitter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#if PROXY_SUPPORT
5+
46
using System;
5-
using System.Linq;
67
using System.Reflection;
78
using System.Reflection.Emit;
89

@@ -140,3 +141,5 @@ public static Func<object, object, bool> CreateProxyMethod(MethodInfo method, Ty
140141
}
141142
}
142143
}
144+
145+
#endif

src/Microsoft.Framework.Notification/Internal/ProxyTypeEmitter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#if PROXY_SUPPORT
5+
46
using System;
57
using System.Collections.Generic;
68
using System.Diagnostics;
@@ -438,3 +440,5 @@ private class VerificationResult
438440
}
439441
}
440442
}
443+
444+
#endif
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#if PROXY_SUPPORT
5+
46
using System;
57
using System.Reflection;
68

79
namespace Microsoft.Framework.Notification
810
{
9-
public class NotifierMethodAdapter : INotifierMethodAdapter
11+
public class ProxyNotifierMethodAdapter : INotifierMethodAdapter
1012
{
1113
public Func<object, object, bool> Adapt(MethodInfo method, Type inputType)
1214
{
1315
return Internal.ProxyMethodEmitter.CreateProxyMethod(method, inputType);
1416
}
1517
}
1618
}
19+
20+
#endif
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
// We only need ReflectionNotifierMethodAdapter for netcore50 or any case where we can't use Reflection.Emit.
5+
//
6+
// However to test it, we compile it into each test assembly. This lets us write tests without a separate
7+
// flavor of tests for netcore50.
8+
#if !PROXY_SUPPORT || TEST
9+
10+
using System;
11+
using System.Reflection;
12+
13+
namespace Microsoft.Framework.Notification
14+
{
15+
public class ReflectionNotifierMethodAdapter : INotifierMethodAdapter
16+
{
17+
public Func<object, object, bool> Adapt(MethodInfo method, Type inputType)
18+
{
19+
return CreateReflectionAdapter(method, inputType);
20+
}
21+
22+
private static Func<object, object, bool> CreateReflectionAdapter(MethodInfo method, Type inputType)
23+
{
24+
var parameters = method.GetParameters();
25+
26+
var mappings = new PropertyInfo[parameters.Length];
27+
28+
var inputTypeInfo = inputType.GetTypeInfo();
29+
for (var i = 0; i < parameters.Length; i++)
30+
{
31+
var parameter = parameters[i];
32+
var property = inputTypeInfo.GetDeclaredProperty(parameter.Name);
33+
if (property == null)
34+
{
35+
continue;
36+
}
37+
else if (parameter.ParameterType.IsAssignableFrom(property.PropertyType))
38+
{
39+
mappings[i] = property;
40+
}
41+
}
42+
43+
return (instance, input) =>
44+
{
45+
if (input.GetType() != inputType)
46+
{
47+
return false;
48+
}
49+
50+
var arguments = new object[mappings.Length];
51+
for (var j = 0; j < mappings.Length; j++)
52+
{
53+
var mapping = mappings[j];
54+
var parameter = parameters[j];
55+
if (mapping == null)
56+
{
57+
if (parameter.ParameterType.GetTypeInfo().IsValueType)
58+
{
59+
arguments[j] = Activator.CreateInstance(parameter.ParameterType);
60+
}
61+
}
62+
else
63+
{
64+
arguments[j] = mapping.GetValue(input);
65+
}
66+
}
67+
68+
method.Invoke(instance, arguments);
69+
return true;
70+
};
71+
}
72+
}
73+
}
74+
75+
#endif

src/Microsoft.Framework.Notification/ServiceCollectionExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ public static class ServiceCollectionExtensions
99
{
1010
public static IServiceCollection AddNotifier(this IServiceCollection services)
1111
{
12-
services.TryAdd(ServiceDescriptor.Singleton(typeof(INotifierMethodAdapter), typeof(NotifierMethodAdapter)));
13-
services.TryAdd(ServiceDescriptor.Singleton(typeof(INotifier), typeof(Notifier)));
12+
#if PROXY_SUPPORT
13+
services.TryAddSingleton<INotifierMethodAdapter, ProxyNotifierMethodAdapter>();
14+
#else
15+
services.TryAddSingleton<INotifierMethodAdapter, ReflectionNotifierMethodAdapter>();
16+
#endif
17+
services.TryAddSingleton<INotifier, Notifier>();
1418
return services;
1519
}
1620
}
Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
{
22
"version": "1.0.0-*",
3-
"description": "Logging infrastructure.",
3+
"description": "An event notification system.",
44
"dependencies": {
55
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-*",
6-
"Microsoft.Framework.NotNullAttribute.Sources": { "version": "1.0.0-*", "type" : "build" }
6+
"Microsoft.Framework.NotNullAttribute.Sources": {
7+
"version": "1.0.0-*",
8+
"type": "build"
9+
}
710
},
811
"compilationOptions": {
912
"define": [
@@ -12,18 +15,14 @@
1215
},
1316
"frameworks": {
1417
"net45": {
15-
"frameworkAssemblies": {
16-
"System.Linq": "",
17-
"System.Collections.Concurrent": ""
18-
}
19-
},
20-
"dnx451": {
18+
"compilationOptions": { "define": [ "PROXY_SUPPORT" ] },
2119
"frameworkAssemblies": {
2220
"System.Linq": "",
2321
"System.Collections.Concurrent": ""
2422
}
2523
},
2624
"dotnet": {
25+
"compilationOptions": { "define": [ "PROXY_SUPPORT" ] },
2726
"dependencies": {
2827
"System.ComponentModel": "4.0.0-beta-*",
2928
"System.Collections.Concurrent": "4.0.10-beta-*",
@@ -33,7 +32,7 @@
3332
"System.Linq": "4.0.0-beta-*",
3433
"System.Linq.Expressions": "4.0.10-beta-*",
3534
"System.Threading": "4.0.10-beta-*",
36-
"System.Reflection": "4.0.10-beta-*",
35+
"System.Reflection": "4.0.10-beta-*",
3736
"System.Reflection.Emit": "4.0.0-beta-*",
3837
"System.Reflection.Emit.Lightweight": "4.0.0-beta-*",
3938
"System.Reflection.Extensions": "4.0.0-beta-*",
@@ -42,6 +41,24 @@
4241
"System.Runtime": "4.0.20-beta-*",
4342
"System.Runtime.Extensions": "4.0.10-beta-*"
4443
}
44+
},
45+
"netcore50": {
46+
"dependencies": {
47+
"System.ComponentModel": "4.0.0-beta-*",
48+
"System.Collections.Concurrent": "4.0.10-beta-*",
49+
"System.Collections": "4.0.10-beta-*",
50+
"System.Diagnostics.Debug": "4.0.10-beta-*",
51+
"System.Globalization": "4.0.10-beta-*",
52+
"System.Linq": "4.0.0-beta-*",
53+
"System.Linq.Expressions": "4.0.10-beta-*",
54+
"System.Threading": "4.0.10-beta-*",
55+
"System.Reflection": "4.0.10-beta-*",
56+
"System.Reflection.Extensions": "4.0.0-beta-*",
57+
"System.Reflection.TypeExtensions": "4.0.0-beta-*",
58+
"System.Resources.ResourceManager": "4.0.0-beta-*",
59+
"System.Runtime": "4.0.20-beta-*",
60+
"System.Runtime.Extensions": "4.0.10-beta-*"
61+
}
4562
}
4663
}
4764
}

test/Microsoft.Framework.Notification.Test/NotifierTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public SomeValueType(int value)
200200

201201
private static INotifier CreateNotifier()
202202
{
203-
return new Notifier(new NotifierMethodAdapter());
203+
return new Notifier(new ProxyNotifierMethodAdapter());
204204
}
205205
}
206206
}

test/Microsoft.Framework.Notification.Test/NotifierMethodAdapterTest.cs renamed to test/Microsoft.Framework.Notification.Test/ProxyNotifierMethodAdapterTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
namespace Microsoft.Framework.Notification
1010
{
11-
public class NotifierMethodAdapterTest
11+
public class ProxyNotifierMethodAdapterTest
1212
{
1313
[Fact]
1414
public void Adapt_ReturnsTrueForTypeMatch()
1515
{
1616
// Arrange
17-
var adapter = new NotifierMethodAdapter();
17+
var adapter = new ProxyNotifierMethodAdapter();
1818

1919
var listener = new Listener1();
2020
var method = GetMethodInfo<Listener1>(l => l.Listen());
@@ -30,7 +30,7 @@ public void Adapt_ReturnsTrueForTypeMatch()
3030
public void Adapt_ReturnsFalseForTypeNotMatching()
3131
{
3232
// Arrange
33-
var adapter = new NotifierMethodAdapter();
33+
var adapter = new ProxyNotifierMethodAdapter();
3434

3535
var listener = new Listener1();
3636
var method = GetMethodInfo<Listener1>(l => l.Listen());
@@ -46,7 +46,7 @@ public void Adapt_ReturnsFalseForTypeNotMatching()
4646
public void Adapt_SplatsParameters()
4747
{
4848
// Arrange
49-
var adapter = new NotifierMethodAdapter();
49+
var adapter = new ProxyNotifierMethodAdapter();
5050

5151
var listener = new Listener2();
5252
var value = new { id = 17, name = "Bill" };
@@ -65,7 +65,7 @@ public void Adapt_SplatsParameters()
6565
public void Adapt_SplatsParameters_ExtraEventDataIgnored()
6666
{
6767
// Arrange
68-
var adapter = new NotifierMethodAdapter();
68+
var adapter = new ProxyNotifierMethodAdapter();
6969

7070
var listener = new Listener2();
7171
var value = new { id = 17, name = "Bill", ignored = "hi" };
@@ -84,7 +84,7 @@ public void Adapt_SplatsParameters_ExtraEventDataIgnored()
8484
public void Adapt_SplatsParameters_ExtraParametersGetDefaultValues()
8585
{
8686
// Arrange
87-
var adapter = new NotifierMethodAdapter();
87+
var adapter = new ProxyNotifierMethodAdapter();
8888

8989
var listener = new Listener2();
9090
var value = new { };
@@ -103,7 +103,7 @@ public void Adapt_SplatsParameters_ExtraParametersGetDefaultValues()
103103
public void Adapt_SplatsParameters_WithProxy()
104104
{
105105
// Arrange
106-
var adapter = new NotifierMethodAdapter();
106+
var adapter = new ProxyNotifierMethodAdapter();
107107

108108
var listener = new Listener3();
109109
var value = new { id = 17, person = new Person() { Name = "Bill" } };

0 commit comments

Comments
 (0)