This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static bool Verify(string digits, string secret) | |
{ | |
if (string.IsNullOrEmpty(digits) || string.IsNullOrEmpty(secret)) | |
return false; | |
long iterations = DateTimeOffset.Now.ToUnixTimeSeconds() / 30L; | |
byte[] secretBytes = Base32ToBytes(secret); | |
if (Verify(digits, secretBytes, iterations)) | |
return true; | |
else |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyVoid | |
{ | |
public static readonly MyVoid Instance = new MyVoid(); | |
private MyVoid() | |
{ | |
unsafe | |
{ | |
MyVoid it = this; | |
TypedReference typedReference = __makeref(it); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object obj = new List<int> { 1, 2, 3 }; | |
TypedReference trObj = __makeref(obj); | |
IntPtr ptrObj = **(IntPtr**)&trObj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A little bit faster then standard Type.GetInterfaces() | |
public static Type[] GetInterfaces(object Obj) | |
{ | |
unsafe | |
{ | |
TypedReference trObj = __makeref(Obj); | |
IntPtr ptrObj = **(IntPtr**)&trObj; | |
void* methodTable = (*(IntPtr*)ptrObj.ToPointer()).ToPointer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A little bit faster then standard Type.GetInterfaces() | |
public static Type[] GetInterfaces(Type ObjectType) | |
{ | |
unsafe | |
{ | |
void* methodTable = ObjectType.TypeHandle.Value.ToPointer(); | |
int count = ((ushort*)methodTable)[7]; | |
IntPtr* interfaces = (IntPtr*)((IntPtr*)methodTable)[9].ToPointer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It's twice slower then `Obj is IMyInterface` | |
// But it's twice faster then any other check | |
// Based on my SO answer: https://ru.stackoverflow.com/a/945958/248572 | |
public static bool Implements(Type ObjectType, Type InterfaceType) | |
{ | |
unsafe | |
{ | |
IntPtr iMTPointer = InterfaceType.TypeHandle.Value; | |
void* methodTable = ObjectType.TypeHandle.Value.ToPointer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It's twice slower then `Obj is IMyInterface` | |
// But it's twice faster then any other check | |
// Based on my SO answer: https://ru.stackoverflow.com/a/945958/248572 | |
public static bool Implements(object Obj, Type InterfaceType) | |
{ | |
unsafe | |
{ | |
// MT Pointer of our interface | |
IntPtr iMTPointer = InterfaceType.TypeHandle.Value; |