Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Aspects.Universal to use Nullable #213

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions samples/src/Universal/Aspects.Universal.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageTags>aspect proxy wrapper universal</PackageTags>
<Description>
This package provides simple universal method wrrapper aspect.
This package provides simple universal method wrapper aspect.
</Description>
<PackageProjectUrl>https://github.com/pamidur/aspect-injector/tree/master/samples/src/Universal</PackageProjectUrl>
<LangVersion>10</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Update="AspectInjector" Version="2.8.1" />
</ItemGroup>

</Project>
22 changes: 11 additions & 11 deletions samples/src/Universal/Aspects/BaseUniversalWrapperAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -19,15 +19,15 @@ public abstract class BaseUniversalWrapperAspect
private static readonly ConcurrentDictionary<(MethodBase, Type), Lazy<Handler>> _delegateCache = new ConcurrentDictionary<(MethodBase, Type), Lazy<Handler>>();

private static readonly MethodInfo _asyncGenericHandler =
typeof(BaseUniversalWrapperAttribute).GetMethod(nameof(BaseUniversalWrapperAttribute.WrapAsync), BindingFlags.NonPublic | BindingFlags.Instance);
typeof(BaseUniversalWrapperAttribute).GetMethod(nameof(BaseUniversalWrapperAttribute.WrapAsync), BindingFlags.NonPublic | BindingFlags.Instance)!;

private static readonly MethodInfo _syncGenericHandler =
typeof(BaseUniversalWrapperAttribute).GetMethod(nameof(BaseUniversalWrapperAttribute.WrapSync), BindingFlags.NonPublic | BindingFlags.Instance);
typeof(BaseUniversalWrapperAttribute).GetMethod(nameof(BaseUniversalWrapperAttribute.WrapSync), BindingFlags.NonPublic | BindingFlags.Instance)!;

private static readonly Type _voidTaskResult = Type.GetType("System.Threading.Tasks.VoidTaskResult");
private static readonly Type _voidTaskResult = Type.GetType("System.Threading.Tasks.VoidTaskResult")!;

protected object BaseHandle(
object instance,
object? instance,
Type type,
MethodBase method,
Func<object[], object> target,
Expand All @@ -53,7 +53,7 @@ protected object BaseHandle(
return handler(target, args, eventArgs);
}

private Handler CreateMethodHandler(Type returnType, IReadOnlyList<BaseUniversalWrapperAttribute> wrappers)
private static Handler CreateMethodHandler(Type returnType, IReadOnlyList<BaseUniversalWrapperAttribute> wrappers)
{
var targetParam = Expression.Parameter(typeof(Func<object[], object>), "orig");
var eventArgsParam = Expression.Parameter(typeof(AspectEventArgs), "event");
Expand All @@ -75,24 +75,24 @@ private Handler CreateMethodHandler(Type returnType, IReadOnlyList<BaseUniversal
wrapperMethod = _syncGenericHandler.MakeGenericMethod(new[] { returnType });
}

var converArgs = Expression.Parameter(typeof(object[]), "args");
var next = Expression.Lambda(Expression.Convert(Expression.Invoke(targetParam, converArgs), returnType), converArgs);
var convertArgs = Expression.Parameter(typeof(object[]), "args");
var next = Expression.Lambda(Expression.Convert(Expression.Invoke(targetParam, convertArgs), returnType), convertArgs);

foreach (var wrapper in wrappers)
{
var argsParam = Expression.Parameter(typeof(object[]), "args");
next = Expression.Lambda(Expression.Call(Expression.Constant(wrapper), wrapperMethod, next, argsParam, eventArgsParam), argsParam);
}

var orig_args = Expression.Parameter(typeof(object[]), "orig_args");
var handler = Expression.Lambda<Handler>(Expression.Convert(Expression.Invoke(next, orig_args), typeof(object)), targetParam, orig_args, eventArgsParam);
var originalArgs = Expression.Parameter(typeof(object[]), "orig_args");
var handler = Expression.Lambda<Handler>(Expression.Convert(Expression.Invoke(next, originalArgs), typeof(object)), targetParam, originalArgs, eventArgsParam);

var handlerCompiled = handler.Compile();

return handlerCompiled;
}

private Handler GetMethodHandler(MethodBase method, Type returnType, IReadOnlyList<BaseUniversalWrapperAttribute> wrappers)
private static Handler GetMethodHandler(MethodBase method, Type returnType, IReadOnlyList<BaseUniversalWrapperAttribute> wrappers)
{
var lazyHandler = _delegateCache.GetOrAdd((method, returnType), _ => new Lazy<Handler>(() => CreateMethodHandler(returnType, wrappers)));
return lazyHandler.Value;
Expand Down
6 changes: 3 additions & 3 deletions samples/src/Universal/Aspects/MethodWrapperAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using AspectInjector.Broker;

Expand All @@ -9,7 +9,7 @@ public class MethodWrapperAspect : BaseUniversalWrapperAspect
{
[Advice(Kind.Around, Targets = Target.Method)]
public object Handle(
[Argument(Source.Instance)] object instance,
[Argument(Source.Instance)] object? instance,
[Argument(Source.Type)] Type type,
[Argument(Source.Metadata)] MethodBase method,
[Argument(Source.Target)] Func<object[], object> target,
Expand All @@ -21,4 +21,4 @@ public object Handle(
return BaseHandle(instance, type, method, target, name, args, returnType, triggers);
}
}
}
}
6 changes: 3 additions & 3 deletions samples/src/Universal/Aspects/PrivateMethodWrapperAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using AspectInjector.Broker;

Expand All @@ -9,7 +9,7 @@ public class PrivateMethodWrapperAspect : BaseUniversalWrapperAspect
{
[Advice(Kind.Around, Targets = Target.Private | Target.Method)]
public object Handle(
[Argument(Source.Instance)] object instance,
[Argument(Source.Instance)] object? instance,
[Argument(Source.Type)] Type type,
[Argument(Source.Metadata)] MethodBase method,
[Argument(Source.Target)] Func<object[], object> target,
Expand All @@ -21,4 +21,4 @@ public object Handle(
return BaseHandle(instance, type, method, target, name, args, returnType, triggers);
}
}
}
}
6 changes: 3 additions & 3 deletions samples/src/Universal/Aspects/PublicMethodWrapperAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using AspectInjector.Broker;

Expand All @@ -9,7 +9,7 @@ public class PublicMethodWrapperAspect : BaseUniversalWrapperAspect
{
[Advice(Kind.Around, Targets = Target.Public | Target.Method)]
public object Handle(
[Argument(Source.Instance)] object instance,
[Argument(Source.Instance)] object? instance,
[Argument(Source.Type)] Type type,
[Argument(Source.Metadata)] MethodBase method,
[Argument(Source.Target)] Func<object[], object> target,
Expand All @@ -21,4 +21,4 @@ public object Handle(
return BaseHandle(instance, type, method, target, name, args, returnType, triggers);
}
}
}
}
6 changes: 3 additions & 3 deletions samples/src/Universal/Aspects/PublicWrapperAspect.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using AspectInjector.Broker;

Expand All @@ -9,7 +9,7 @@ public class PublicWrapperAspect : BaseUniversalWrapperAspect
{
[Advice(Kind.Around, Targets = Target.Public | Target.Any)]
public object Handle(
[Argument(Source.Instance)] object instance,
[Argument(Source.Instance)] object? instance,
[Argument(Source.Type)] Type type,
[Argument(Source.Metadata)] MethodBase method,
[Argument(Source.Target)] Func<object[], object> target,
Expand All @@ -21,4 +21,4 @@ public object Handle(
return BaseHandle(instance, type, method, target, name, args, returnType, triggers);
}
}
}
}
18 changes: 9 additions & 9 deletions samples/src/Universal/Events/AspectEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Aspects.Universal.Events
{
public class AspectEventArgs : EventArgs
{
public object Instance { get; internal set; }
public Type Type { get; internal set; }
public MethodBase Method { get; internal set; }
public string Name { get; internal set; }
public IReadOnlyList<object> Args { get; internal set; }
public Type ReturnType { get; internal set; }
public Attribute[] Triggers { get; internal set; }
public object? Instance { get; internal set; }
public Type Type { get; internal set; } = null!;
public MethodBase Method { get; internal set; } = null!;
public string Name { get; internal set; } = null!;
public IReadOnlyList<object> Args { get; internal set; } = null!;
public Type ReturnType { get; internal set; } = null!;
public Attribute[] Triggers { get; internal set; } = null!;
}
}
}