Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Jul 21, 2023
1 parent 632ccf7 commit 301d80b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
56 changes: 22 additions & 34 deletions JLChnToZ.CommonUtils.Dynamic/Limitless.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,67 +234,55 @@ public override bool Equals(object obj) {
public static bool operator ==(Limitless a, Limitless b) {
if (ReferenceEquals(a, b)) return true;
object result;
if (a.TryInvoke(true, "op_Equality", out result, a.target, b.target) ||
b.TryInvoke(true, "op_Equality", out result, a.target, b.target))
if (a.TryInvokeSafe(true, "op_Equality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Equality", out result, a, b))
return (bool)result;
return ReferenceEquals(a.target, b.target);
return ReferenceEquals(a?.target, b?.target);
}

public static bool operator !=(Limitless a, Limitless b) {
if (ReferenceEquals(a, b)) return false;
object result;
if (a.TryInvoke(true, "op_Inequality", out result, a.target, b.target) ||
b.TryInvoke(true, "op_Inequality", out result, a.target, b.target))
if (a.TryInvokeSafe(true, "op_Inequality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Inequality", out result, a, b))
return (bool)result;
return !ReferenceEquals(a.target, b.target);
return !ReferenceEquals(a?.target, b?.target);
}

public static bool operator ==(Limitless a, object b) {
if (b is Limitless wrapped) {
if (ReferenceEquals(a, wrapped)) return true;
b = wrapped.target;
}
if (ReferenceEquals(a, b)) return true;
object result;
if (a.TryInvoke(true, "op_Equality", out result, a.target, b) ||
b.TryInvoke(true, "op_Equality", out result, a.target, b))
if (a.TryInvokeSafe(true, "op_Equality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Equality", out result, a, b))
return (bool)result;
return ReferenceEquals(a.target, b);
return ReferenceEquals(a?.target, b);
}

public static bool operator !=(Limitless a, object b) {
if (b is Limitless wrapped) {
if (ReferenceEquals(a, wrapped)) return false;
b = wrapped.target;
}
if (ReferenceEquals(a, b)) return false;
object result;
if (a.TryInvoke(true, "op_Inequality", out result, a.target, b) ||
b.TryInvoke(true, "op_Inequality", out result, a.target, b))
if (a.TryInvokeSafe(true, "op_Inequality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Inequality", out result, a, b))
return (bool)result;
return !ReferenceEquals(a.target, b);
return !ReferenceEquals(a?.target, b);
}

public static bool operator ==(object a, Limitless b) {
if (a is Limitless wrapped) {
if (ReferenceEquals(wrapped, b)) return true;
a = wrapped.target;
}
if (ReferenceEquals(a, b)) return true;
object result;
if (a.TryInvoke(true, "op_Equality", out result, a, b.target) ||
b.TryInvoke(true, "op_Equality", out result, a, b.target))
if (a.TryInvokeSafe(true, "op_Equality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Equality", out result, a, b))
return (bool)result;
return ReferenceEquals(a, b.target);
return ReferenceEquals(a, b?.target);
}

public static bool operator !=(object a, Limitless b) {
if (a is Limitless wrapped) {
if (ReferenceEquals(wrapped, b)) return false;
a = wrapped.target;
}
if (ReferenceEquals(a, b)) return false;
object result;
if (a.TryInvoke(true, "op_Inequality", out result, a, b.target) ||
b.TryInvoke(true, "op_Inequality", out result, a, b.target))
if (a.TryInvokeSafe(true, "op_Inequality", out result, a, b) ||
b.TryInvokeSafe(true, "op_Inequality", out result, a, b))
return (bool)result;
return !ReferenceEquals(a, b.target);
return !ReferenceEquals(a, b?.target);
}
}
}
2 changes: 1 addition & 1 deletion JLChnToZ.CommonUtils.Dynamic/LimitlessEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected override MethodInfo[] MethodInfos {
get {
methodInfos[0] = eventInfo.GetUndelyRaiseMethod(out backingDelegate, target);
if (methodInfos[0] == null)
throw new InvalidOperationException("Event does not have a raise method.");
throw new InvalidOperationException("Raise method unavailable for this context.");
return methodInfos;
}
}
Expand Down
17 changes: 14 additions & 3 deletions JLChnToZ.CommonUtils.Dynamic/LimitlessInvokable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public dynamic Invoke(params object[] args) {
}

public override bool TryConvert(ConvertBinder binder, out object result) {
if (target != null && binder.Type.IsAssignableFrom(target.GetType())) {
result = target;
return true;
}
if (TryCreateDelegate(binder.Type, out var resultDelegate)) {
result = resultDelegate;
return true;
Expand All @@ -102,9 +106,16 @@ internal protected bool TryCreateDelegate(Type delegateType, out Delegate result
) as MethodInfo;
if (matched != null && matched.ReturnType == invokeMethod.ReturnType) {
var target = InvokeTarget;
result = matched.IsStatic ?
Delegate.CreateDelegate(delegateType, matched, false) :
Delegate.CreateDelegate(delegateType, target, matched, false);
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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions JLChnToZ.CommonUtils.Dynamic/Utilites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static string ToOperatorMethodName(this ExpressionType expressionType) {
}
}

public static bool TryInvoke(this object obj, bool isStatic, string methodName, out object result, params object[] args) {
public static bool TryInvokeSafe(this object obj, bool isStatic, string methodName, out object result, params object[] args) {
if (obj == null) {
result = null;
return false;
Expand All @@ -109,8 +109,8 @@ public static MethodInfo GetUndelyRaiseMethod(this EventInfo eventInfo, out Dele
if (backingField != null) {
var backingFieldType = backingField.FieldType;
if (backingFieldType.IsSubclassOf(typeof(Delegate))) {
raiseMethod = backingFieldType.GetMethod("Invoke");
backingDelegate = backingField.GetValue(instance) as Delegate;
if (backingDelegate != null) raiseMethod = backingFieldType.GetMethod("Invoke");
}
}
}
Expand Down

0 comments on commit 301d80b

Please sign in to comment.