private MemberInfo ResolveTypeMemberRef(Type type, string name, ByteReader sig, Type[] genericTypeArguments, Type[] genericMethodArguments) { IGenericContext context; if ((genericTypeArguments == null && genericMethodArguments == null) || type.IsGenericType) { context = type; } else { context = new GenericContext(genericTypeArguments, genericMethodArguments); } if (sig.PeekByte() == Signature.FIELD) { Type org = type; FieldSignature fieldSig = FieldSignature.ReadSig(this, sig, context); do { FieldInfo field = type.FindField(name, fieldSig); if (field != null) { return(field); } type = type.BaseType; } while (type != null); throw new MissingFieldException(org.ToString(), name); } else { Type org = type; MethodSignature methodSig = MethodSignature.ReadSig(this, sig, context); do { MethodBase method = type.FindMethod(name, methodSig); if (method != null) { return(method); } type = type.BaseType; } while (type != null); return(universe.GetMissingMethodOrThrow(org, name, methodSig)); } }
private MemberInfo ResolveTypeMemberRef(Type type, string name, ByteReader sig) { if (sig.PeekByte() == Signature.FIELD) { Type org = type; FieldSignature fieldSig = FieldSignature.ReadSig(this, sig, type); FieldInfo field = type.FindField(name, fieldSig); if (field == null && universe.MissingMemberResolution) { return(universe.GetMissingFieldOrThrow(type, name, fieldSig)); } while (field == null && (type = type.BaseType) != null) { field = type.FindField(name, fieldSig); } if (field != null) { return(field); } throw new MissingFieldException(org.ToString(), name); } else { Type org = type; MethodSignature methodSig = MethodSignature.ReadSig(this, sig, type); MethodBase method = type.FindMethod(name, methodSig); if (method == null && universe.MissingMemberResolution) { return(universe.GetMissingMethodOrThrow(type, name, methodSig)); } while (method == null && (type = type.BaseType) != null) { method = type.FindMethod(name, methodSig); } if (method != null) { return(method); } throw new MissingMethodException(org.ToString(), name); } }
private MemberInfo ResolveTypeMemberRef(Type type, string name, ByteReader sig) { if (sig.PeekByte() == Signature.FIELD) { Type org = type; FieldSignature fieldSig = FieldSignature.ReadSig(this, sig, type); FieldInfo field = type.FindField(name, fieldSig); if (field == null && universe.MissingMemberResolution) { return universe.GetMissingFieldOrThrow(this, type, name, fieldSig); } while (field == null && (type = type.BaseType) != null) { field = type.FindField(name, fieldSig); } if (field != null) { return field; } throw new MissingFieldException(org.ToString(), name); } else { Type org = type; MethodSignature methodSig = MethodSignature.ReadSig(this, sig, type); MethodBase method = type.FindMethod(name, methodSig); if (method == null && universe.MissingMemberResolution) { return universe.GetMissingMethodOrThrow(this, type, name, methodSig); } while (method == null && (type = type.BaseType) != null) { method = type.FindMethod(name, methodSig); } if (method != null) { return method; } throw new MissingMethodException(org.ToString(), name); } }
internal static __StandAloneMethodSig ReadStandAloneMethodSig(ModuleReader module, ByteReader br, IGenericContext context) { CallingConventions callingConvention = 0; System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention = 0; bool unmanaged; byte flags = br.ReadByte(); switch (flags & 7) { case DEFAULT: callingConvention = CallingConventions.Standard; unmanaged = false; break; case 0x01: // C unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl; unmanaged = true; break; case 0x02: // STDCALL unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall; unmanaged = true; break; case 0x03: // THISCALL unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.ThisCall; unmanaged = true; break; case 0x04: // FASTCALL unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.FastCall; unmanaged = true; break; case VARARG: callingConvention = CallingConventions.VarArgs; unmanaged = false; break; default: throw new BadImageFormatException(); } if ((flags & HASTHIS) != 0) { callingConvention |= CallingConventions.HasThis; } if ((flags & EXPLICITTHIS) != 0) { callingConvention |= CallingConventions.ExplicitThis; } if ((flags & GENERIC) != 0) { throw new BadImageFormatException(); } int paramCount = br.ReadCompressedInt(); SkipCustomModifiers(br); Type returnType = ReadRetType(module, br, context); List<Type> parameterTypes = new List<Type>(); List<Type> optionalParameterTypes = new List<Type>(); List<Type> curr = parameterTypes; for (int i = 0; i < paramCount; i++) { if (br.PeekByte() == SENTINEL) { br.ReadByte(); curr = optionalParameterTypes; } SkipCustomModifiers(br); curr.Add(ReadParam(module, br, context)); } return new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray()); }
internal static MethodSignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context) { CallingConventions callingConvention; int genericParamCount; Type returnType; Type[] parameterTypes; byte flags = br.ReadByte(); switch (flags & 7) { case DEFAULT: callingConvention = CallingConventions.Standard; break; case VARARG: callingConvention = CallingConventions.VarArgs; break; default: throw new BadImageFormatException(); } if ((flags & HASTHIS) != 0) { callingConvention |= CallingConventions.HasThis; } if ((flags & EXPLICITTHIS) != 0) { callingConvention |= CallingConventions.ExplicitThis; } genericParamCount = 0; if ((flags & GENERIC) != 0) { genericParamCount = br.ReadCompressedInt(); context = new UnboundGenericMethodContext(context); } int paramCount = br.ReadCompressedInt(); Type[][][] modifiers = null; Type[] optionalCustomModifiers; Type[] requiredCustomModifiers; ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers); returnType = ReadRetType(module, br, context); parameterTypes = new Type[paramCount]; PackedCustomModifiers.SetModifiers(ref modifiers, 0, 0, optionalCustomModifiers, paramCount + 1); PackedCustomModifiers.SetModifiers(ref modifiers, 0, 1, requiredCustomModifiers, paramCount + 1); for (int i = 0; i < parameterTypes.Length; i++) { if ((callingConvention & CallingConventions.VarArgs) != 0 && br.PeekByte() == SENTINEL) { Array.Resize(ref parameterTypes, i); if (modifiers != null) { Array.Resize(ref modifiers, i + 1); } break; } ReadCustomModifiers(module, br, context, out requiredCustomModifiers, out optionalCustomModifiers); PackedCustomModifiers.SetModifiers(ref modifiers, i + 1, 0, optionalCustomModifiers, paramCount + 1); PackedCustomModifiers.SetModifiers(ref modifiers, i + 1, 1, requiredCustomModifiers, paramCount + 1); parameterTypes[i] = ReadParam(module, br, context); } return new MethodSignature(returnType, parameterTypes, modifiers, callingConvention, genericParamCount); }
protected static Type ReadParam(ModuleReader module, ByteReader br, IGenericContext context) { switch (br.PeekByte()) { case ELEMENT_TYPE_TYPEDBYREF: br.ReadByte(); return module.universe.System_TypedReference; default: return ReadTypeOrByRef(module, br, context); } }
// this reads just the optional parameter types, from a MethodRefSig internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br, IGenericContext context, out CustomModifiers[] customModifiers) { br.ReadByte(); int paramCount = br.ReadCompressedUInt(); CustomModifiers.Skip(br); ReadRetType(module, br, context); for (int i = 0; i < paramCount; i++) { if (br.PeekByte() == SENTINEL) { br.ReadByte(); Type[] types = new Type[paramCount - i]; customModifiers = new CustomModifiers[types.Length]; for (int j = 0; j < types.Length; j++) { customModifiers[j] = CustomModifiers.Read(module, br, context); types[j] = ReadType(module, br, context); } return types; } CustomModifiers.Skip(br); ReadType(module, br, context); } customModifiers = Empty<CustomModifiers>.Array; return Type.EmptyTypes; }
internal static void ReadLocalVarSig(ModuleReader module, ByteReader br, IGenericContext context, List<LocalVariableInfo> list) { if (br.Length < 2 || br.ReadByte() != LOCAL_SIG) { throw new BadImageFormatException("Invalid local variable signature"); } int count = br.ReadCompressedUInt(); for (int i = 0; i < count; i++) { if (br.PeekByte() == ELEMENT_TYPE_TYPEDBYREF) { br.ReadByte(); list.Add(new LocalVariableInfo(i, module.universe.System_TypedReference, false, new CustomModifiers())); } else { CustomModifiers mods1 = CustomModifiers.Read(module, br, context); bool pinned = false; if (br.PeekByte() == ELEMENT_TYPE_PINNED) { br.ReadByte(); pinned = true; } CustomModifiers mods2 = CustomModifiers.Read(module, br, context); Type type = ReadTypeOrByRef(module, br, context); list.Add(new LocalVariableInfo(i, type, pinned, CustomModifiers.Combine(mods1, mods2))); } } }
private static Type ReadTypeOrByRef(ModuleReader module, ByteReader br, IGenericContext context) { if (br.PeekByte() == ELEMENT_TYPE_BYREF) { br.ReadByte(); // LAMESPEC it is allowed (by C++/CLI, ilasm and peverify) to have custom modifiers after the BYREF // (which makes sense, as it is analogous to pointers) CustomModifiers mods = CustomModifiers.Read(module, br, context); // C++/CLI generates void& local variables, so we need to use ReadTypeOrVoid here return ReadTypeOrVoid(module, br, context).__MakeByRefType(mods); } else { return ReadType(module, br, context); } }
internal static void ReadDeclarativeSecurity(Assembly asm, List<CustomAttributeData> list, int action, ByteReader br) { Universe u = asm.universe; if (br.PeekByte() == '.') { br.ReadByte(); int count = br.ReadCompressedInt(); for (int j = 0; j < count; j++) { Type type = ReadType(asm, br); ConstructorInfo constructor = type.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction); // LAMESPEC there is an additional length here (probably of the named argument list) byte[] blob = br.ReadBytes(br.ReadCompressedInt()); list.Add(new CustomAttributeData(asm, constructor, action, blob)); } } else { // .NET 1.x format (xml) char[] buf = new char[br.Length / 2]; for (int i = 0; i < buf.Length; i++) { buf[i] = br.ReadChar(); } string xml = new String(buf); ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetPseudoCustomAttributeConstructor(u.System_Security_Permissions_SecurityAction); List<CustomAttributeNamedArgument> args = new List<CustomAttributeNamedArgument>(); args.Add(new CustomAttributeNamedArgument(GetProperty(u.System_Security_Permissions_PermissionSetAttribute, "XML", u.System_String), new CustomAttributeTypedArgument(u.System_String, xml))); list.Add(new CustomAttributeData(asm.ManifestModule, constructor, new object[] { action }, args)); } }
private static Type ReadTypeOrVoid(ModuleReader module, ByteReader br, IGenericContext context) { if (br.PeekByte() == ELEMENT_TYPE_VOID) { br.ReadByte(); return module.universe.System_Void; } else { return ReadType(module, br, context); } }
protected static void ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context, out Type[] requiredCustomModifiers, out Type[] optionalCustomModifiers) { byte b = br.PeekByte(); if (IsCustomModifier(b)) { List<Type> required = new List<Type>(); List<Type> optional = new List<Type>(); while (IsCustomModifier(b)) { br.ReadByte(); Type type = ReadTypeDefOrRefEncoded(module, br, context); if (b == ELEMENT_TYPE_CMOD_REQD) { required.Add(type); } else { optional.Add(type); } b = br.PeekByte(); } requiredCustomModifiers = required.ToArray(); optionalCustomModifiers = optional.ToArray(); } else { requiredCustomModifiers = null; optionalCustomModifiers = null; } }
// this reads just the optional parameter types, from a MethodRefSig internal static Type[] ReadOptionalParameterTypes(ModuleReader module, ByteReader br) { br.ReadByte(); int paramCount = br.ReadCompressedInt(); SkipCustomModifiers(br); ReadRetType(module, br, null); for (int i = 0; i < paramCount; i++) { if (br.PeekByte() == SENTINEL) { br.ReadByte(); Type[] types = new Type[paramCount - i]; for (int j = 0; j < types.Length; j++) { SkipCustomModifiers(br); types[j] = ReadType(module, br, null); } return types; } SkipCustomModifiers(br); ReadType(module, br, null); } return Type.EmptyTypes; }
protected static void SkipCustomModifiers(ByteReader br) { byte b = br.PeekByte(); while (IsCustomModifier(b)) { br.ReadByte(); br.ReadCompressedInt(); b = br.PeekByte(); } }
private static CustomModifiers ReadCustomModifiers(ModuleReader module, ByteReader br, IGenericContext context) { CustomModifiers mods = new CustomModifiers(); byte b = br.PeekByte(); if (IsCustomModifier(b)) { List<Type> required = new List<Type>(); List<Type> optional = new List<Type>(); while (IsCustomModifier(b)) { bool req = br.ReadByte() == ELEMENT_TYPE_CMOD_REQD; Type type = ReadTypeDefOrRefEncoded(module, br, context); (req ? required : optional).Add(type); b = br.PeekByte(); } mods.required = required.ToArray(); mods.optional = optional.ToArray(); } return mods; }
internal static void ReadDeclarativeSecurity(Assembly asm, List<CustomAttributeData> list, int action, ByteReader br) { Universe u = asm.universe; if (br.PeekByte() == '.') { br.ReadByte(); int count = br.ReadCompressedInt(); for (int j = 0; j < count; j++) { Type type = ReadType(asm, br); ConstructorInfo constructor; if (type == u.System_Security_Permissions_HostProtectionAttribute && action == (int)System.Security.Permissions.SecurityAction.LinkDemand) { constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); } else { constructor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[] { u.System_Security_Permissions_SecurityAction }, null); } // LAMESPEC there is an additional length here (probably of the named argument list) ByteReader slice = br.Slice(br.ReadCompressedInt()); // LAMESPEC the count of named arguments is a compressed integer (instead of UInt16 as NumNamed in custom attributes) list.Add(new CustomAttributeData(constructor, action, ReadNamedArguments(asm, slice, slice.ReadCompressedInt(), type))); } } else { // .NET 1.x format (xml) char[] buf = new char[br.Length / 2]; for (int i = 0; i < buf.Length; i++) { buf[i] = br.ReadChar(); } string xml = new String(buf); ConstructorInfo constructor = u.System_Security_Permissions_PermissionSetAttribute.GetConstructor(new Type[] { u.System_Security_Permissions_SecurityAction }); List<CustomAttributeNamedArgument> args = new List<CustomAttributeNamedArgument>(); args.Add(new CustomAttributeNamedArgument(u.System_Security_Permissions_PermissionSetAttribute.GetProperty("XML"), new CustomAttributeTypedArgument(u.System_String, xml))); list.Add(new CustomAttributeData(constructor, action, args)); } }
private MemberInfo ResolveTypeMemberRef(Type type, string name, ByteReader sig, Type[] genericTypeArguments, Type[] genericMethodArguments) { IGenericContext context; if ((genericTypeArguments == null && genericMethodArguments == null) || type.IsGenericType) { context = type; } else { context = new GenericContext(genericTypeArguments, genericMethodArguments); } if (sig.PeekByte() == Signature.FIELD) { Type org = type; FieldSignature fieldSig = FieldSignature.ReadSig(this, sig, context); do { FieldInfo field = type.FindField(name, fieldSig); if (field != null) { return field; } type = type.BaseType; } while (type != null); throw new MissingFieldException(org.ToString(), name); } else { Type org = type; MethodSignature methodSig = MethodSignature.ReadSig(this, sig, context); do { MethodBase method = type.FindMethod(name, methodSig); if (method != null) { return method; } type = type.BaseType; } while (type != null); return universe.GetMissingMethodOrThrow(org, name, methodSig); } }
internal static PropertySignature ReadSig(ModuleReader module, ByteReader br, IGenericContext context) { byte flags = br.ReadByte(); if ((flags & PROPERTY) == 0) { throw new BadImageFormatException(); } CallingConventions callingConvention = CallingConventions.Standard; if ((flags & HASTHIS) != 0) { callingConvention |= CallingConventions.HasThis; } if ((flags & EXPLICITTHIS) != 0) { callingConvention |= CallingConventions.ExplicitThis; } Type returnType; Type[] returnTypeRequiredCustomModifiers; Type[] returnTypeOptionalCustomModifiers; Type[] parameterTypes; Type[][] parameterRequiredCustomModifiers; Type[][] parameterOptionalCustomModifiers; int paramCount = br.ReadCompressedInt(); ReadCustomModifiers(module, br, context, out returnTypeRequiredCustomModifiers, out returnTypeOptionalCustomModifiers); returnType = ReadRetType(module, br, context); parameterTypes = new Type[paramCount]; parameterRequiredCustomModifiers = null; parameterOptionalCustomModifiers = null; for (int i = 0; i < parameterTypes.Length; i++) { if (IsCustomModifier(br.PeekByte())) { if (parameterOptionalCustomModifiers == null) { parameterOptionalCustomModifiers = new Type[parameterTypes.Length][]; parameterRequiredCustomModifiers = new Type[parameterTypes.Length][]; } ReadCustomModifiers(module, br, context, out parameterRequiredCustomModifiers[i], out parameterOptionalCustomModifiers[i]); } parameterTypes[i] = ReadParam(module, br, context); } return new PropertySignature(callingConvention, returnType, returnTypeOptionalCustomModifiers, returnTypeRequiredCustomModifiers, parameterTypes, parameterOptionalCustomModifiers, parameterRequiredCustomModifiers); }