/// <summary> /// Add a value for this field /// </summary> /// <param name="val">the value for the field</param> public void AddValue(Constant val) { flags |= HasDefault; constVal = val; }
/// <summary> /// Remove the initial value from this field /// </summary> public void RemoveValue() { constVal = null; flags &= NoDefault; }
internal void AddFieldOrProps(string[] names, Constant[] vals, bool[] isField) { this.names = names; this.vals = vals; this.isField = isField; numNamed = (ushort)names.Length; }
/*----------------------------- internal functions ------------------------------*/ internal void DecodeCustomAttributeBlob() { MemoryStream caBlob = new MemoryStream(byteVal); BinaryReader blob = new BinaryReader(caBlob,System.Text.Encoding.UTF8); if (blob.ReadUInt16() != CustomAttribute.prolog) throw new PEFileException("Invalid Custom Attribute Blob"); Type[] parTypes = type.GetParTypes(); argVals = new Constant[parTypes.Length]; for (int i=0; i < parTypes.Length; i++) { Type argType = parTypes[i]; bool arrayConst = argType is Array; if (arrayConst) argType = ((ZeroBasedArray)(parTypes[i])).ElemType(); bool boxed = argType is SystemClass; int eType = argType.GetTypeIndex(); if (arrayConst) { Constant[] elems = new Constant[blob.ReadUInt32()]; for (int j=0; j < elems.Length; j++) { if (boxed) { eType = blob.ReadByte(); elems[j] = new BoxedSimpleConst((SimpleConstant)PEReader.ReadConst(eType,blob)); } else { elems[j] = PEReader.ReadConst(eType,blob); } } argVals[i] = new ArrayConst(elems); } else if (boxed) { argVals[i] = new BoxedSimpleConst((SimpleConstant)PEReader.ReadConst(blob.ReadByte(),blob)); } else { argVals[i] = PEReader.ReadConst(eType,blob); } } uint numNamed = 0; if (blob.BaseStream.Position != byteVal.Length) numNamed = blob.ReadUInt16(); if (numNamed > 0) { names = new string[numNamed]; vals = new Constant[numNamed]; isField = new bool[numNamed]; for (int i=0; i < numNamed; i++) { isField[i] = blob.ReadByte() == 0x53; int eType = blob.ReadByte(); names[i] = blob.ReadString(); vals[i] = PEReader.ReadConst(eType,blob); } } }
/*-------------------- Constructors ---------------------------------*/ internal CustomAttribute(MetaDataElement paren, Method constrType, Constant[] val) { parent = paren; type = constrType; argVals = val; changed = true; sortTable = true; tabIx = MDTable.CustomAttribute; }
/*------------------------- public set and get methods --------------------------*/ public void AddFieldOrProp(string name, Constant val, bool isFld) { if ((byteVal != null) && !changed) DecodeCustomAttributeBlob(); if (numNamed == 0) { names = new string[initSize]; vals = new Constant[initSize]; isField = new bool[initSize]; } else if (numNamed >= names.Length) { string[] tmpNames = names; Constant[] tmpVals = vals; bool[] tmpField = isField; names = new String[names.Length + initSize]; vals = new Constant[vals.Length + initSize]; isField = new bool[isField.Length + initSize]; for (int i = 0; i < numNamed; i++) { names[i] = tmpNames[i]; vals[i] = tmpVals[i]; isField[i] = tmpField[i]; } } names[numNamed] = name; vals[numNamed] = val; isField[numNamed++] = isFld; changed = true; }
internal ConstantElem(PEReader buff) { byte constType = buff.ReadByte(); byte pad = buff.ReadByte(); parentIx = buff.GetCodedIndex(CIx.HasConstant); //valIx = buff.GetBlobIx(); cValue = buff.GetBlobConst(constType); sortTable = true; tabIx = MDTable.Constant; }
/*-------------------- Constructors ---------------------------------*/ public ArrayConst(Constant[] elems) { type = ElementType.SZArray; size = 5; // one byte for SZARRAY, 4 bytes for length elements = elems; for (int i=0; i < elements.Length; i++) { size += elements[i].GetSize(); } }
public void RemoveInitValue() { constVal = null; }
/*-------------------- Constructors ---------------------------------*/ internal ConstantElem(MetaDataElement parent, Constant val) { this.parent = parent; cValue = val; sortTable = true; tabIx = MDTable.Constant; }
/// <summary> /// Add an initial value for this property /// </summary> /// <param name="constVal">the initial value for this property</param> public void AddInitValue(Constant constVal) { this.constVal = constVal; }
/// <summary> /// Makes the assembly debuggable by attaching the DebuggableAttribute /// to the Assembly. Call immediately before calling WritePEFile. /// </summary> /// <param name="allowDebug">set true to enable debugging, false otherwise</param> /// <param name="suppressOpt">set true to disable optimizations that affect debugging</param> public void MakeDebuggable(bool allowDebug, bool suppressOpt) { ClassRef debugRef = null; MethodRef dCtor = null; Type[] twoBools = new Type[] { PrimitiveType.Boolean, PrimitiveType.Boolean }; debugRef = MSCorLib.mscorlib.GetClass("System.Diagnostics", "DebuggableAttribute"); if (debugRef == null) debugRef = MSCorLib.mscorlib.AddClass("System.Diagnostics", "DebuggableAttribute"); dCtor = debugRef.GetMethod(".ctor", twoBools); if (dCtor == null) { dCtor = debugRef.AddMethod(".ctor", PrimitiveType.Void, twoBools); dCtor.AddCallConv(CallConv.Instance); } Constant[] dbgArgs = new Constant[] { new BoolConst(allowDebug), new BoolConst(suppressOpt) }; thisAssembly.AddCustomAttribute(dCtor, dbgArgs); }
/// <summary> /// Remove the default constant value for this parameter /// </summary> public void RemoveDefaultValue() { defaultVal = null; parMode &= noDefault; }
/// <summary> /// Add a default value to this parameter /// </summary> /// <param name="cVal">the default value for the parameter</param> public void AddDefaultValue(Constant cVal) { defaultVal = cVal; parMode |= hasDefault; }
/// <summary> /// Add a custom attribute to this item /// </summary> /// <param name="ctorMeth">the constructor method for this attribute</param> /// <param name="cVals">the constant values of the parameters</param> public void AddCustomAttribute(Method ctorMeth, Constant[] cVals) { if (customAttributes == null) { customAttributes = new ArrayList(); } customAttributes.Add(new CustomAttribute(this,ctorMeth,cVals)); }