public static void RuntimeMethodHandleTest() { MethodInfo mi = typeof(Base).GetMethod(nameof(Base.MyMethod)); RuntimeMethodHandle h = mi.MethodHandle; Assert.Equal(mi, MethodBase.GetMethodFromHandle(h)); Assert.True(h.Value != IntPtr.Zero); Assert.Equal(h.GetHashCode(), h.GetHashCode()); Assert.Equal(default(RuntimeMethodHandle).GetHashCode(), default(RuntimeMethodHandle).GetHashCode()); Assert.True(h.Equals(h)); Assert.False(h.Equals(default(RuntimeMethodHandle))); Assert.False(default(RuntimeMethodHandle).Equals(h)); Assert.True(default(RuntimeMethodHandle).Equals(default(RuntimeMethodHandle))); Assert.True(h.Equals((object)h)); Assert.True(((IEquatable <RuntimeMethodHandle>)h).Equals(h)); Assert.False(h.Equals(new object())); Assert.False(h.Equals(null)); Assert.False(h == default(RuntimeMethodHandle)); Assert.True(h != default(RuntimeMethodHandle)); }
public void Equals_RuntimeMethodHandle() { Type type = typeof(RuntimeMethodHandleTest); RuntimeMethodHandle rmhA1 = type.GetMethod("Equals_Object").MethodHandle; RuntimeMethodHandle rmhA2 = type.GetMethod("Equals_Object").MethodHandle; RuntimeMethodHandle rmhB1 = type.GetMethod("Equals_RuntimeMethodHandle").MethodHandle; RuntimeMethodHandle rmhB2 = type.GetMethod("Equals_RuntimeMethodHandle").MethodHandle; Assert.IsTrue(rmhA1.Equals(rmhA1), "#1"); Assert.IsTrue(rmhA1.Equals(rmhA2), "#2"); Assert.IsTrue(rmhB1.Equals(rmhB1), "#3"); Assert.IsTrue(rmhB2.Equals(rmhB1), "#4"); Assert.IsFalse(rmhA1.Equals(rmhB1), "#5"); Assert.IsFalse(rmhB1.Equals(rmhA1), "#6"); }
public static void RuntimeMethodHandleTest() { MethodInfo mi = typeof(Base).GetMethod(nameof(Base.MyMethod)); RuntimeMethodHandle h = mi.MethodHandle; Assert.Equal(mi, MethodBase.GetMethodFromHandle(h)); Assert.True(h.Value != IntPtr.Zero); Assert.Equal(h.GetHashCode(), h.GetHashCode()); Assert.Equal(default(RuntimeMethodHandle).GetHashCode(), default(RuntimeMethodHandle).GetHashCode()); Assert.True(h.Equals(h)); Assert.False(h.Equals(default(RuntimeMethodHandle))); Assert.False(default(RuntimeMethodHandle).Equals(h)); Assert.True(default(RuntimeMethodHandle).Equals(default(RuntimeMethodHandle))); Assert.True(h.Equals((object)h)); Assert.True(((IEquatable <RuntimeMethodHandle>)h).Equals(h)); Assert.False(h.Equals(new object())); Assert.False(h.Equals(null)); Assert.False(h == default(RuntimeMethodHandle)); Assert.True(h != default(RuntimeMethodHandle)); IntPtr hPtr = RuntimeMethodHandle.ToIntPtr(h); RuntimeMethodHandle hNew = RuntimeMethodHandle.FromIntPtr(hPtr); Assert.True(h.Equals(hNew)); Assert.True(hNew.Equals(h)); // Confirm the created handle is valid Assert.Equal(mi, MethodBase.GetMethodFromHandle(hNew)); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify different instances are equal when they have the same values..."); try { //default value is zero RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); if (!myHandle.Equals(myRuntimeMethodHandle)) { TestLibrary.TestFramework.LogError("003","The two instance should be different!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004","Unexpected exception occurs: " + e); retVal = false; } return retVal; }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify clone instance is equal to it original..."); try { RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); object cloneHandle = myHandle; if (!myHandle.Equals(cloneHandle)) { TestLibrary.TestFramework.LogError("001","The two instances should be equal!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002","Unexpected exception occurs: " + e); retVal = false; } return retVal; }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify RuntimeMethodHandle instance is not equal to common object instance..."); try { RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); if (myHandle.Equals(myObj)) { TestLibrary.TestFramework.LogError("005", "The two instances should not be equal!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception occurs: " + e); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify different instances are equal when they have the same values..."); try { //default value is zero RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); if (!myHandle.Equals(myRuntimeMethodHandle)) { TestLibrary.TestFramework.LogError("003", "The two instance should be different!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception occurs: " + e); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify clone instance is equal to it original..."); try { RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); object cloneHandle = myHandle; if (!myHandle.Equals(cloneHandle)) { TestLibrary.TestFramework.LogError("001", "The two instances should be equal!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpected exception occurs: " + e); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("Verify RuntimeMethodHandle instance is not equal to common object instance..."); try { RuntimeMethodHandle myHandle = new RuntimeMethodHandle(); if (myHandle.Equals(myObj)) { TestLibrary.TestFramework.LogError("005","The two instances should not be equal!"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006","Unexpected exception occurs: " + e); retVal = false; } return retVal; }
public override bool Equals(object?obj) { return(obj is RuntimeHandleTuple vTuple && MethodHandle.Equals(vTuple.MethodHandle) && TypeHandle.Equals(vTuple.TypeHandle)); }