Skip to content

Commit

Permalink
change delegate creation implements
Browse files Browse the repository at this point in the history
after 50 builds, let codecov send notification
  • Loading branch information
loning committed Mar 2, 2020
1 parent 5f16df3 commit 01b89fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
3 changes: 3 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
codecov:
notify:
after_n_builds: 50
59 changes: 38 additions & 21 deletions src/AElf.Runtime.CSharp/CSharpSmartContractProxy.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using AElf.CSharp.CodeOps;
using AElf.Sdk.CSharp;
using AElf.Types;

namespace AElf.Runtime.CSharp
Expand All @@ -17,29 +13,50 @@ private static MethodInfo GetMethodInfo(Type type, string name)
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
}


private static Delegate CreateDelegate(object instance, MethodInfo method)
// private static Delegate CreateDelegate(object instance, MethodInfo method)
// {
// return Delegate.CreateDelegate
// (
// Expression.GetDelegateType
// (
// method.GetParameters()
// .Select(p => p.ParameterType)
// .Concat(new Type[] {method.ReturnType})
// .ToArray()
// ),
// instance,
// method
// );
// }
//
//
// private static Delegate CreateDelegate(object instance, Type type, string name)
// {
// var methodInfo = GetMethodInfo(type, name);
// return methodInfo == null ? null : CreateDelegate(instance, methodInfo);
// }

private static T CreateDelegate<T>(object instance, MethodInfo method)
where T : Delegate
{
return Delegate.CreateDelegate
return (T) Delegate.CreateDelegate
(
Expression.GetDelegateType
(
method.GetParameters()
.Select(p => p.ParameterType)
.Concat(new Type[] {method.ReturnType})
.ToArray()
),
typeof(T),
instance,
method
);
}

private static Delegate CreateDelegate(object instance, Type type, string name)
private static T CreateDelegate<T>(object instance, Type type, string name)
where T : Delegate
{
var methodInfo = GetMethodInfo(type, name);
return methodInfo == null ? null : CreateDelegate(instance, methodInfo);

return methodInfo == null ? null : CreateDelegate<T>(instance, methodInfo);
}



private readonly Action _methodCleanup;
private readonly Func<TransactionExecutingStateSet> _methodGetChanges;
private readonly Action<ISmartContractBridgeContext> _methodInternalInitialize;
Expand All @@ -52,20 +69,20 @@ public CSharpSmartContractProxy(object instance, Type counterType)
{
var instanceType = instance.GetType();

_methodCleanup = (Action) CreateDelegate(instance, instanceType, nameof(Cleanup));
_methodCleanup = CreateDelegate<Action>(instance, instanceType, nameof(Cleanup));

_methodGetChanges =
(Func<TransactionExecutingStateSet>) CreateDelegate(instance, instanceType, nameof(GetChanges));
CreateDelegate<Func<TransactionExecutingStateSet>>(instance, instanceType, nameof(GetChanges));

_methodInternalInitialize =
(Action<ISmartContractBridgeContext>) CreateDelegate(instance, instanceType,
CreateDelegate<Action<ISmartContractBridgeContext>>(instance, instanceType,
nameof(InternalInitialize));

_methodResetFields = (Action) CreateDelegate(instance, instanceType, nameof(ResetFields));
_methodResetFields = CreateDelegate<Action>(instance, instanceType, nameof(ResetFields));

_methodSetExecutionObserver = counterType == null
? null
: (Action<IExecutionObserver>) CreateDelegate(null,
: CreateDelegate<Action<IExecutionObserver>>(null,
counterType?.GetMethod(nameof(ExecutionObserverProxy.SetObserver),
new[] {typeof(IExecutionObserver)}));
}
Expand Down

0 comments on commit 01b89fa

Please sign in to comment.