Skip to content

Commit

Permalink
Fix misused IsAssignedFrom
Browse files Browse the repository at this point in the history
Some refactoring
  • Loading branch information
JLChnToZ committed May 30, 2023
1 parent 444c189 commit 6a56fa1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions JLChnToZ.CommonUtils.Dynamic/Limitless.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Dynamic;
using System.Linq.Expressions;

namespace JLChnToZ.CommonUtils.Dynamic {
using static Utilites;
Expand Down Expand Up @@ -138,11 +137,11 @@ public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes) {
if (indexes.Length == 1) {
if (type.IsArray) return false;
if (type.IsAssignableFrom(typeof(IDictionary))) {
if (typeof(IDictionary).IsAssignableFrom(type)) {
((IDictionary)target).Remove(indexes[0]);
return true;
}
if (type.IsAssignableFrom(typeof(IList)) && indexes[0] != null && indexes[0].GetType() == typeof(int)) {
if (typeof(IList).IsAssignableFrom(type) && indexes[0] != null && indexes[0].GetType() == typeof(int)) {
((IList)target).RemoveAt(Convert.ToInt32(indexes[0]));
return true;
}
Expand All @@ -153,7 +152,7 @@ public override bool TryDeleteIndex(DeleteIndexBinder binder, object[] indexes)
}

public override bool TryDeleteMember(DeleteMemberBinder binder) {
if (type.IsAssignableFrom(typeof(IDictionary))) {
if (typeof(IDictionary).IsAssignableFrom(type)) {
((IDictionary)target).Remove(binder.Name);
return true;
}
Expand Down Expand Up @@ -229,16 +228,18 @@ public override bool Equals(object obj) {
public static bool operator ==(Limitless a, Limitless b) {
if (ReferenceEquals(a.target, b.target)) return true;
object result;
if (a.typeInfo.TryInvoke(null, "op_Equality", new[] { a.target, b.target }, out result)) return (bool)result;
if (b.typeInfo.TryInvoke(null, "op_Equality", new[] { a.target, b.target }, out result)) return (bool)result;
if (a.typeInfo.TryInvoke(null, "op_Equality", new[] { a.target, b.target }, out result) ||
b.typeInfo.TryInvoke(null, "op_Equality", new[] { a.target, b.target }, out result))
return (bool)result;
return false;
}

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

Expand Down
2 changes: 1 addition & 1 deletion JLChnToZ.CommonUtils.Dynamic/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ internal bool TryCast(object instance, Type type, bool isIn, out object result)
return !(isIn ? this.type : type).IsValueType; // Only reference types can be null
}
var instanceType = isIn ? this.type : instance.GetType();
if (instanceType.IsAssignableFrom(type) || type.IsAssignableFrom(instanceType)) { // No need to cast if the type is already assignable
if (type.IsAssignableFrom(instanceType)) { // No need to cast if the type is already assignable
result = instance;
return true;
}
Expand Down

0 comments on commit 6a56fa1

Please sign in to comment.