public static void *IsInstanceOfInterfaceType(int interfaceSlot, void *obj) { MDTypeDefinition *objTypeDefinition = (MDTypeDefinition *)((uint *)obj)[0]; if (objTypeDefinition == null) { return(null); } uint *bitmap = objTypeDefinition->Bitmap; if (bitmap == null) { return(null); } int index = interfaceSlot / 32; int bit = interfaceSlot % 32; uint value = bitmap[index]; uint result = value & (uint)(1 << bit); if (result == 0) { return(null); } return(obj); }
internal RuntimeType(RuntimeTypeHandle handle) { this.handle = handle; typeDefinition = (MDTypeDefinition *)((uint **)&handle)[0]; assemblyQualifiedName = typeDefinition->Name; // TODO name = typeDefinition->Name; // TODO @namespace = typeDefinition->Name; // TODO fullname = typeDefinition->Name; typeCode = typeDefinition->TypeCode; attributes = typeDefinition->Attributes; // Declaring Type if (typeDefinition->DeclaringType != null) { RuntimeTypeHandle declaringHandle = new RuntimeTypeHandle(); ((uint **)&declaringHandle)[0] = (uint *)typeDefinition->DeclaringType; declaringTypeHandle = declaringHandle; } // Element Type if (typeDefinition->ElementType != null) { RuntimeTypeHandle elementHandle = new RuntimeTypeHandle(); ((uint **)&elementHandle)[0] = (uint *)typeDefinition->ElementType; elementTypeHandle = elementHandle; } }
public static bool IsTypeInInheritanceChain(MDTypeDefinition *typeDefinition, MDTypeDefinition *chain) { while (chain != null) { if (chain == typeDefinition) { return(true); } chain = chain->ParentType; } return(false); }
public RuntimeCustomAttributeData(MDCustomAttribute *customAttributeTable) { RuntimeTypeHandle typeHandle = new RuntimeTypeHandle(); ((uint **)&typeHandle)[0] = (uint *)customAttributeTable->AttributeType; base.attributeType = Type.GetTypeFromHandle(typeHandle); // Get the metadata pointer for the enum type typeHandle = typeof(System.Enum).TypeHandle; EnumTypePtr = (MDTypeDefinition *)((uint **)&typeHandle)[0]; // Create temporary lists to hold the arguments var typedArgs = new LinkedList <CustomAttributeTypedArgument>(); var namedArgs = new LinkedList <CustomAttributeNamedArgument>(); for (uint i = 0; i < customAttributeTable->NumberOfArguments; i++) { // Get the argument metadata pointer var argument = customAttributeTable->GetCustomAttributeArgument(i); // Get the argument name (if any) string name = argument->Name; // Get the argument type RuntimeTypeHandle argTypeHandle = new RuntimeTypeHandle(); ((uint **)&argTypeHandle)[0] = (uint *)argument->ArgumentType; var argType = Type.GetTypeFromHandle(argTypeHandle); // Get the argument value var value = ResolveArgumentValue(argument, argType); // If the argument has a name then its a NamedArgument, otherwise its a TypedArgument if (name == null) { typedArgs.AddLast(CreateTypedArgumentStruct(argType, value)); } else { namedArgs.AddLast(CreateNamedArgumentStruct(name, argType, value, argument->IsField)); } } // Generate arrays from the argument lists ctorArgs = typedArgs.ToArray(); this.namedArgs = namedArgs.ToArray(); }
public static void *IsInstanceOfType(RuntimeTypeHandle handle, void *obj) { if (obj == null) { return(null); } MDTypeDefinition *typeDefinition = (MDTypeDefinition *)((uint **)&handle)[0]; MDTypeDefinition *objTypeDefinition = (MDTypeDefinition *)((uint *)obj)[0]; if (IsTypeInInheritanceChain(typeDefinition, objTypeDefinition)) { return(obj); } return(null); }
public RuntimeTypeInfo(RuntimeType type, Assembly assembly) { var handle = type.TypeHandle; asType = type; this.assembly = assembly; //this.handle = handle; typeDefinition = (MDTypeDefinition *)((uint **)&handle)[0]; assemblyQualifiedName = typeDefinition->Name; // TODO name = typeDefinition->Name; // TODO @namespace = typeDefinition->Name; // TODO fullname = typeDefinition->Name; typeCode = typeDefinition->TypeCode; attributes = typeDefinition->Attributes; // Base Type if (typeDefinition->ParentType != null) { RuntimeTypeHandle parentHandle = new RuntimeTypeHandle(); ((uint **)&parentHandle)[0] = (uint *)typeDefinition->ParentType; baseType = Type.GetTypeFromHandle(parentHandle); } // Declaring Type if (typeDefinition->DeclaringType != null) { RuntimeTypeHandle declaringHandle = new RuntimeTypeHandle(); ((uint **)&declaringHandle)[0] = (uint *)typeDefinition->DeclaringType; declaringType = Type.GetTypeFromHandle(declaringHandle); } // Element Type if (typeDefinition->ElementType != null) { RuntimeTypeHandle elementHandle = new RuntimeTypeHandle(); ((uint **)&elementHandle)[0] = (uint *)typeDefinition->ElementType; elementType = Type.GetTypeFromHandle(elementHandle); } }
private static void Copy(void *sourceArray, int sourceIndex, void *destinationArray, int destinationIndex, int length, bool reliable) { // TODO: add more checks, allow type upcasting, add multi dimensional array support if (sourceArray == null) { throw new ArgumentNullException(nameof(sourceArray)); } if (destinationArray == null) { throw new ArgumentNullException(nameof(destinationArray)); } if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length)); } var sourceLength = GetLength(sourceArray, 0); var destinationLength = GetLength(destinationArray, 0); if (sourceLength - sourceIndex < length) { throw new ArgumentOutOfRangeException(nameof(length)); } if (destinationLength - destinationIndex < length) { throw new ArgumentOutOfRangeException(nameof(length)); } // Get type info MDTypeDefinition *typeStruct = (MDTypeDefinition *)((uint *)sourceArray); var typeCode = typeStruct->TypeCode; var size = (typeCode == TypeCode.ReferenceType) ? Ptr.Size : typeStruct->Size; Internal.MemoryCopy(((Ptr)destinationArray + (Ptr.Size * 2) + (destinationIndex * size)), ((Ptr)sourceArray + (Ptr.Size * 2) + (sourceIndex * size)), (uint)(length * size)); }
public static MDProtectedRegionDefinition *GetProtectedRegionEntryByAddress(uint address, MDTypeDefinition *exceptionType, MDMethodDefinition *methodDef) { var protectedRegionTable = methodDef->ProtectedRegionTable; if (protectedRegionTable == null) { return(null); } uint method = (uint)methodDef->Method; if (method == 0) { return(null); } uint offset = address - method; uint entries = protectedRegionTable->NumberOfRegions; uint entry = 0; MDProtectedRegionDefinition *protectedRegionDef = null; uint currentStart = uint.MinValue; uint currentEnd = uint.MaxValue; while (entry < entries) { var prDef = protectedRegionTable->GetProtectedRegionDefinition(entry); uint start = prDef->StartOffset; uint end = prDef->EndOffset; if ((offset >= start) && (offset < end) && (start >= currentStart) && (end < currentEnd)) { var handlerType = prDef->HandlerType; var exType = prDef->ExceptionType; // If the handler is a finally clause, accept without testing // If the handler is a exception clause, accept if the exception type is in the is within the inheritance chain of the exception object if ((handlerType == ExceptionHandlerType.Finally) || (handlerType == ExceptionHandlerType.Exception && IsTypeInInheritanceChain(exType, exceptionType))) { protectedRegionDef = prDef; currentStart = start; currentEnd = end; } } entry++; } return(protectedRegionDef); }