Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Dec 29, 2023
1 parent 9b5d4c9 commit 4ba4662
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletion JLChnToZ.CommonUtils.Dynamic.Test/TestSubject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace JLChnToZ.CommonUtils.Dynamic.Test {
#pragma warning disable CS0067
#pragma warning disable CS0414
#pragma warning disable CS0660
#pragma warning disable CS0661

namespace JLChnToZ.CommonUtils.Dynamic.Test {
internal class TestSubject: IInterface {
public static string lastCalledMethod;

Expand Down
4 changes: 2 additions & 2 deletions JLChnToZ.CommonUtils.Dynamic/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static Delegate BindMethod(Type type, string methodName, BindingFlags bindingFla
TypeInfo.Get(type).TryGetMethods(methodName, out var methods) &&
TryGetDelegateSignature(delegateType, out var parameters, out _) ? BindMethod(
type, delegateType,
Type.DefaultBinder.SelectMethod(bindingFlags, methods, parameters, null) as MethodInfo,
SelectMethod(methods, parameters, bindingFlags),
target
) : null;

Expand All @@ -134,7 +134,7 @@ static Delegate BindProperty(Type type, BindingFlags bindingFlags, string proper
var typeInfo = TypeInfo.Get(type);
PropertyInfo selectedProperty;
if (parameters.Length > 0) {
selectedProperty = Type.DefaultBinder.SelectProperty(bindingFlags, typeInfo.indexers, returnType, parameters, null);
selectedProperty = SelectIndexer(typeInfo.indexers, parameters, returnType, bindingFlags);
if (selectedProperty == null) return null;
} else if (!typeInfo.TryGetProperties(propertyName, out selectedProperty))
return null;
Expand Down
31 changes: 14 additions & 17 deletions JLChnToZ.CommonUtils.Dynamic/LimitlessInvokable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,22 @@ public Delegate CreateDelegate(Type delegateType) {
public T CreateDelegate<T>() where T : Delegate => CreateDelegate(typeof(T)) as T;

internal protected bool TryCreateDelegate(Type delegateType, out Delegate result) {
if (delegateType.IsSubclassOf(typeof(Delegate))) {
if (TryGetDelegateSignature(delegateType, out var parameters, out var returnType)) {
var matched = Type.DefaultBinder.SelectMethod(
DEFAULT_FLAGS, MethodInfos, parameters, null
) as MethodInfo;
if (matched != null && matched.ReturnType == returnType) {
var target = InvokeTarget;
if (matched.IsStatic) {
result = Delegate.CreateDelegate(delegateType, matched, false);
return result != null;
}
var declaringType = matched.DeclaringType;
if (declaringType.IsSubclassOf(typeof(Delegate)) && matched.Name == "Invoke") {
result = (target as Delegate).ConvertAndFlatten(delegateType);
return result != null;
}
result = Delegate.CreateDelegate(delegateType, target, matched, false);
if (delegateType.IsSubclassOf(typeof(Delegate)) &&
TryGetDelegateSignature(delegateType, out var parameters, out var returnType)) {
var matched = SelectMethod(MethodInfos, parameters);
if (matched != null && matched.ReturnType == returnType) {
var target = InvokeTarget;
if (matched.IsStatic) {
result = Delegate.CreateDelegate(delegateType, matched, false);
return result != null;
}
var declaringType = matched.DeclaringType;
if (declaringType.IsSubclassOf(typeof(Delegate)) && matched.Name == "Invoke") {
result = (target as Delegate).ConvertAndFlatten(delegateType);
return result != null;
}
result = Delegate.CreateDelegate(delegateType, target, matched, false);
return result != null;
}
}
result = null;
Expand Down
20 changes: 4 additions & 16 deletions JLChnToZ.CommonUtils.Dynamic/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,9 @@ internal bool TryGetValue(object instance, object[] indexes, out object value) {
value = null;
return false;
}
var binder = Type.DefaultBinder;
var matched = binder.SelectProperty(
DEFAULT_FLAGS | BindingFlags.GetProperty, indexers, null, Array.ConvertAll(indexes, GetUndelyType), null
);
var matched = SelectIndexer(indexers, Array.ConvertAll(indexes, GetUndelyType), null, DEFAULT_FLAGS | BindingFlags.GetProperty);
if (matched != null) {
value = InternalWrap(matched.GetValue(instance, InternalUnwrap(
indexes, binder,
matched.GetIndexParameters().ToParameterTypes()
)));
value = InternalWrap(matched.GetValue(instance, InternalUnwrap(indexes, matched.GetIndexParameters())));
return true;
}
value = null;
Expand All @@ -150,15 +144,9 @@ internal bool TrySetValue(object instance, string key, object value) {

internal bool TrySetValue(object instance, object[] indexes, object value) {
if (indexes == null || indexers.Length == 0) return false;
var binder = Type.DefaultBinder;
var matched = binder.SelectProperty(
DEFAULT_FLAGS | BindingFlags.SetProperty, indexers, null, Array.ConvertAll(indexes, GetUndelyType), null
);
var matched = SelectIndexer(indexers, Array.ConvertAll(indexes, GetUndelyType), null, DEFAULT_FLAGS | BindingFlags.SetProperty);
if (matched != null) {
matched.SetValue(InternalUnwrap(instance), value, InternalUnwrap(
indexes, binder,
matched.GetIndexParameters().ToParameterTypes()
));
matched.SetValue(InternalUnwrap(instance), value, InternalUnwrap(indexes, matched.GetIndexParameters()));
return true;
}
return false;
Expand Down
29 changes: 15 additions & 14 deletions JLChnToZ.CommonUtils.Dynamic/Utilites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ internal static class Utilites {
public const BindingFlags DEFAULT_FLAGS = STATIC_FLAGS | INSTANCE_FLAGS;
public static readonly object[] emptyArgs = new object[0];

public static object InternalUnwrap(object obj, Binder binder = null, Type requestedType = null) {
public static object InternalUnwrap(object obj, Type requestedType = null) {
if (obj == null) return null;
if (obj is Limitless limitObj) obj = limitObj.target;
if (requestedType != null) {
try {
if (binder != null) return binder.ChangeType(obj, requestedType, null);
return Type.DefaultBinder.ChangeType(obj, requestedType, null);
} catch {}
try {
return Convert.ChangeType(obj, requestedType);
Expand All @@ -25,9 +25,9 @@ public static object InternalUnwrap(object obj, Binder binder = null, Type reque
return obj;
}

public static object[] InternalUnwrap(object[] objs, Binder binder = null, Type[] requestedTypes = null) {
for (int i = 0; i < objs.Length; i++) objs[i] = InternalUnwrap(objs[i], binder, requestedTypes != null ? requestedTypes[i] : null);
return objs;
public static object[] InternalUnwrap(object[] args, ParameterInfo[] parameterInfos = null) {
for (int i = 0; i < args.Length; i++) args[i] = InternalUnwrap(args[i], parameterInfos?[i]?.ParameterType);
return args;
}

public static object InternalWrap(object obj, Type type = null) =>
Expand All @@ -41,17 +41,20 @@ public static void InternalWrap(object[] sourceObj, object[] destObj) {

public static bool TryGetMatchingMethod<T>(T[] methodInfos, ref object[] args, out T bestMatches) where T : MethodBase {
if (args == null) args = emptyArgs;
var binder = Type.DefaultBinder;
var matched = binder.SelectMethod(DEFAULT_FLAGS, methodInfos, Array.ConvertAll(args, GetUndelyType), null);
if (matched != null) {
bestMatches = (T)matched;
args = InternalUnwrap(args, binder, matched.GetParameters().ToParameterTypes());
bestMatches = SelectMethod(methodInfos, Array.ConvertAll(args, GetUndelyType));
if (bestMatches != null) {
args = InternalUnwrap(args, bestMatches.GetParameters());
return true;
}
bestMatches = null;
return false;
}

public static TMethod SelectMethod<TMethod>(TMethod[] methodInfos, Type[] parameters, BindingFlags bindingFlags = DEFAULT_FLAGS) where TMethod : MethodBase =>
Type.DefaultBinder.SelectMethod(bindingFlags, methodInfos, parameters, null) as TMethod;

public static PropertyInfo SelectIndexer(PropertyInfo[] indexers, Type[] parameters, Type returnType = null, BindingFlags bindingFlags = DEFAULT_FLAGS) =>
Type.DefaultBinder.SelectProperty(bindingFlags, indexers, returnType, parameters, null);

public static Type GetUndelyType(object obj) {
if (obj == null) return typeof(object);
if (obj is Limitless limitObj) return limitObj.type;
Expand All @@ -70,13 +73,11 @@ public static bool TryGetDelegateSignature(Type delegateType, out Type[] paramet
returnType = null;
return false;
}
parameters = invokeMethod.GetParameters().ToParameterTypes();
parameters = Array.ConvertAll(invokeMethod.GetParameters(), GetParameterType);
returnType = invokeMethod.ReturnType;
return true;
}

public static Type[] ToParameterTypes(this ParameterInfo[] parameterInfos) => Array.ConvertAll(parameterInfos, GetParameterType);

static Type GetParameterType(ParameterInfo parameterInfo) => parameterInfo.ParameterType;

public static string ToOperatorMethodName(this ExpressionType expressionType) {
Expand Down

0 comments on commit 4ba4662

Please sign in to comment.