FullNameCreator(bool isReflection, IFullNameCreatorHelper helper, StringBuilder sb) { this.sb = sb ?? new StringBuilder(); this.isReflection = isReflection; this.helper = helper; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); }
CustomAttributeReader(ModuleDef module, IBinaryReader reader, bool ownRerader) { this.module = module; this.reader = reader; this.ownReader = ownRerader; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; }
CustomAttributeReader(ModuleDefMD readerModule, uint offset) { this.module = readerModule; this.reader = readerModule.BlobStream.CreateStream(offset); this.ownReader = true; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; }
CustomAttributeReader(ModuleDef module, IBinaryReader reader, GenericParamContext gpContext) { this.module = module; this.reader = reader; this.ownReader = false; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; this.gpContext = gpContext; }
ModuleDef GetOwnerModule(TypeSig typeSig) { if (typeSig == null) return null; if (!recursionCounter.Increment()) return null; ModuleDef result; var old = genericArguments; typeSig = ReplaceGenericArg(typeSig); switch (typeSig.ElementType) { case ElementType.Void: case ElementType.Boolean: case ElementType.Char: case ElementType.I1: case ElementType.U1: case ElementType.I2: case ElementType.U2: case ElementType.I4: case ElementType.U4: case ElementType.I8: case ElementType.U8: case ElementType.R4: case ElementType.R8: case ElementType.String: case ElementType.TypedByRef: case ElementType.I: case ElementType.U: case ElementType.Object: case ElementType.ValueType: case ElementType.Class: result = GetOwnerModule(((TypeDefOrRefSig)typeSig).TypeDefOrRef); break; case ElementType.Ptr: case ElementType.ByRef: case ElementType.Array: case ElementType.SZArray: case ElementType.CModReqd: case ElementType.CModOpt: case ElementType.Pinned: case ElementType.ValueArray: case ElementType.Module: result = GetOwnerModule(typeSig.Next); break; case ElementType.GenericInst: var genericInstSig = (GenericInstSig)typeSig; var genericType = genericInstSig.GenericType; result = GetOwnerModule(genericType == null ? null : genericType.TypeDefOrRef); break; case ElementType.Var: case ElementType.MVar: case ElementType.FnPtr: case ElementType.Sentinel: case ElementType.End: case ElementType.R: case ElementType.Internal: default: result = null; break; } genericArguments = old; recursionCounter.Decrement(); return result; }
void CreateTypeSigName(TypeSig typeSig, int flags) { if (typeSig == null) { sb.Append(NULLVALUE); return; } if (!recursionCounter.Increment()) { sb.Append(RECURSION_ERROR_RESULT_STRING); return; } var old = genericArguments; typeSig = ReplaceGenericArg(typeSig); bool createNamespace = (flags & TYPESIG_NAMESPACE) != 0; bool createName = (flags & TYPESIG_NAME) != 0; switch (typeSig.ElementType) { case ElementType.Void: case ElementType.Boolean: case ElementType.Char: case ElementType.I1: case ElementType.U1: case ElementType.I2: case ElementType.U2: case ElementType.I4: case ElementType.U4: case ElementType.I8: case ElementType.U8: case ElementType.R4: case ElementType.R8: case ElementType.String: case ElementType.TypedByRef: case ElementType.I: case ElementType.U: case ElementType.Object: case ElementType.ValueType: case ElementType.Class: if (createNamespace && createName) CreateFullName(((TypeDefOrRefSig)typeSig).TypeDefOrRef); else if (createNamespace) CreateNamespace(((TypeDefOrRefSig)typeSig).TypeDefOrRef); else if (createName) CreateName(((TypeDefOrRefSig)typeSig).TypeDefOrRef); break; case ElementType.Ptr: CreateTypeSigName(typeSig.Next, flags); if (createName) sb.Append('*'); break; case ElementType.ByRef: CreateTypeSigName(typeSig.Next, flags); if (createName) sb.Append('&'); break; case ElementType.Array: CreateTypeSigName(typeSig.Next, flags); if (createName) { var arraySig = (ArraySig)typeSig; sb.Append('['); if (arraySig.Rank == 0) sb.Append("<RANK0>"); // Not allowed else if (arraySig.Rank == 1) sb.Append('*'); else for (int i = 0; i < (int)arraySig.Rank; i++) { if (i != 0) sb.Append(','); if (!isReflection) { const int NO_LOWER = int.MinValue; const uint NO_SIZE = uint.MaxValue; int lower = i >= arraySig.LowerBounds.Count ? NO_LOWER : arraySig.LowerBounds[i]; uint size = i >= arraySig.Sizes.Count ? NO_SIZE : arraySig.Sizes[i]; if (lower != NO_LOWER) { sb.Append(lower); sb.Append(".."); if (size != NO_SIZE) sb.Append(lower + (int)size - 1); else sb.Append('.'); } } } sb.Append(']'); } break; case ElementType.SZArray: CreateTypeSigName(typeSig.Next, flags); if (createName) sb.Append("[]"); break; case ElementType.CModReqd: CreateTypeSigName(typeSig.Next, flags); if (!isReflection && createName) { sb.Append(" modreq("); if (createNamespace) CreateFullName(((ModifierSig)typeSig).Modifier); else CreateName(((ModifierSig)typeSig).Modifier); sb.Append(")"); } break; case ElementType.CModOpt: CreateTypeSigName(typeSig.Next, flags); if (!isReflection && createName) { sb.Append(" modopt("); if (createNamespace) CreateFullName(((ModifierSig)typeSig).Modifier); else CreateName(((ModifierSig)typeSig).Modifier); sb.Append(")"); } break; case ElementType.Pinned: CreateTypeSigName(typeSig.Next, flags); break; case ElementType.ValueArray: CreateTypeSigName(typeSig.Next, flags); if (createName) { var valueArraySig = (ValueArraySig)typeSig; sb.Append(" ValueArray("); sb.Append(valueArraySig.Size); sb.Append(')'); } break; case ElementType.Module: CreateTypeSigName(typeSig.Next, flags); if (createName) { var moduleSig = (ModuleSig)typeSig; sb.Append(" Module("); sb.Append(moduleSig.Index); sb.Append(')'); } break; case ElementType.GenericInst: var genericInstSig = (GenericInstSig)typeSig; var typeGenArgs = genericInstSig.GenericArguments; CreateTypeSigName(genericInstSig.GenericType, flags); if (createNamespace && createName) { if (isReflection) { sb.Append('['); for (int i = 0; i < typeGenArgs.Count; i++) { if (i != 0) sb.Append(','); var genArg = typeGenArgs[i]; bool mustWriteAssembly = MustUseAssemblyName(genArg); if (mustWriteAssembly) sb.Append('['); CreateFullName(genArg); if (mustWriteAssembly) { sb.Append(", "); var asm = GetDefinitionAssembly(genArg); if (asm == null) sb.Append(NULLVALUE); else sb.Append(EscapeAssemblyName(GetAssemblyName(asm))); sb.Append(']'); } } sb.Append(']'); } else { sb.Append('<'); for (int i = 0; i < typeGenArgs.Count; i++) { if (i != 0) sb.Append(','); CreateFullName(typeGenArgs[i]); } sb.Append('>'); } } break; case ElementType.Var: if (createName) { sb.Append('!'); sb.Append(((GenericSig)typeSig).Number); } break; case ElementType.MVar: if (createName) { sb.Append("!!"); sb.Append(((GenericSig)typeSig).Number); } break; case ElementType.FnPtr: if (createName) { if (isReflection) sb.Append("(fnptr)"); else CreateMethodFullName(null, null, ((FnPtrSig)typeSig).MethodSig); } break; case ElementType.Sentinel: break; case ElementType.End: case ElementType.R: case ElementType.Internal: default: break; } genericArguments = old; recursionCounter.Decrement(); }
TypeSig ReplaceGenericArg(TypeSig typeSig) { if (genericArguments == null) return typeSig; var newTypeSig = genericArguments.Resolve(typeSig); if (newTypeSig != typeSig) genericArguments = null; return newTypeSig; }
CustomAttribute Read(ICustomAttributeType ctor) { var methodSig = ctor == null ? null : ((IMethodDefOrRef)ctor).MethodSig; if (methodSig == null) throw new CABlobParserException("ctor is null or not a method"); var mrCtor = ctor as MemberRef; if (mrCtor != null) { var owner = mrCtor.Class as TypeSpec; if (owner != null) { var gis = owner.TypeSig as GenericInstSig; if (gis != null) { genericArguments = new GenericArguments(); genericArguments.PushTypeArgs(gis.GenericArguments); } } } bool isEmpty = methodSig.Params.Count == 0 && reader.Position == reader.Length; if (!isEmpty && reader.ReadUInt16() != 1) throw new CABlobParserException("Invalid CA blob prolog"); var ctorArgs = new List<CAArgument>(methodSig.Params.Count); foreach (var arg in methodSig.Params.GetSafeEnumerable()) ctorArgs.Add(ReadFixedArg(FixTypeSig(arg))); // Some tools don't write the next ushort if there are no named arguments. int numNamedArgs = reader.Position == reader.Length ? 0 : reader.ReadUInt16(); var namedArgs = ReadNamedArguments(numNamedArgs); // verifyReadAllBytes will be set when we guess the underlying type of an enum. // To make sure we guessed right, verify that we read all bytes. if (verifyReadAllBytes && reader.Position != reader.Length) throw new CABlobParserException("Not all CA blob bytes were read"); return new CustomAttribute(ctor, ctorArgs, namedArgs); }
CustomAttributeReader(ModuleDef module, IImageStream reader, ICustomAttributeType ctor) { this.module = module; this.reader = reader; this.ownReader = false; this.ctor = ctor; this.genericArguments = null; this.recursionCounter = new RecursionCounter(); this.verifyReadAllBytes = false; }
/// <summary> /// Initializes a new instance of the <see cref="GenericArgumentResolver"/> class. /// </summary> public GenericArgumentResolver() { genericArguments = new GenericArguments(); recursionCounter = new RecursionCounter(); }
private static MethodSig Resolve(MethodBaseSig method, TypeSig typeContext) { var args = new GenericArguments(); if (typeContext.IsGenericInstanceType) args.PushTypeArgs(((GenericInstSig)typeContext).GenericArguments); var retSig = new MethodSig(method.CallingConvention); retSig.RetType = args.Resolve(method.RetType); foreach (var param in method.Params) retSig.Params.Add(args.Resolve(param)); return retSig; }
private static TypeSig Resolve(TypeSig type, TypeSig typeContext) { var args = new GenericArguments(); if (typeContext.IsGenericInstanceType) args.PushTypeArgs(((GenericInstSig)typeContext).GenericArguments); return args.Resolve(type); }
private static IEnumerable<TypeSig> BaseTypes(TypeSig type) { TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDefThrow(); if (typeDef.BaseType == null) yield break; TypeSig baseType = type; do { var args = new GenericArguments(); if (baseType.IsGenericInstanceType) args.PushTypeArgs(((GenericInstSig)baseType).GenericArguments); baseType = args.Resolve(typeDef.BaseType.ToTypeSig()); yield return baseType; typeDef = typeDef.BaseType.ResolveTypeDef(); } while (typeDef != null && typeDef.BaseType != null); }
public static TypeSig SubstituteTypeArgs(TypeSig type, TypeSig typeContext = null, IMethod method = null) { var args = new GenericArguments(); if (typeContext is ArraySig || typeContext is SZArraySig) { args.PushTypeArgs(new[] { typeContext.Next }); } else if (typeContext is GenericInstSig) { args.PushTypeArgs(((GenericInstSig)typeContext).GenericArguments); } if (method is MethodSpec) { args.PushMethodArgs(((MethodSpec)method).GenericInstMethodSig.GenericArguments); } return args.Resolve(type); }