public static bool HasConstant(IHasConstant hc, out CustomAttribute constantAttribute) { constantAttribute = null; if (hc == null) { return(false); } if (hc.Constant != null) { return(true); } foreach (var ca in hc.CustomAttributes) { var type = ca.AttributeType; while (type != null) { var fullName = type.FullName; if (fullName == "System.Runtime.CompilerServices.CustomConstantAttribute" || fullName == "System.Runtime.CompilerServices.DecimalConstantAttribute") { constantAttribute = ca; return(true); } type = type.GetBaseType(); } } return(false); }
void ReadConstants() { if (!m_tHeap.HasTable(ConstantTable.RId)) { return; } ConstantTable csTable = m_tableReader.GetConstantTable(); for (int i = 0; i < csTable.Rows.Count; i++) { ConstantRow csRow = csTable [i]; object constant = GetConstant(csRow.Value, csRow.Type); IHasConstant owner = null; switch (csRow.Parent.TokenType) { case TokenType.Field: owner = GetFieldDefAt(csRow.Parent.RID); break; case TokenType.Property: owner = GetPropertyDefAt(csRow.Parent.RID); break; case TokenType.Param: owner = GetParamDefAt(csRow.Parent.RID); break; } owner.Constant = constant; } }
internal static void SetConstant(this IHasConstant owner, LazyValue <Constant> container, Constant newValue) { if (newValue != null && newValue.Parent != null) { throw new InvalidOperationException("Constant is already added to another member."); } if (container.Value != null) { container.Value.Parent = null; } container.Value = newValue; if (newValue != null) { newValue.Parent = owner; } }
public static bool TryGetConstant(IHasConstant hc, CustomAttribute constantAttribute, out object constant) { if (hc.Constant != null) { constant = hc.Constant.Value; return(true); } if (constantAttribute != null) { if (constantAttribute.TypeFullName == "System.Runtime.CompilerServices.DecimalConstantAttribute") { if (TryGetDecimalConstantAttributeValue(constantAttribute, out var decimalValue)) { constant = decimalValue; return(true); } } } constant = null; return(false); }
void WriteConstant(IHasConstant hc, TypeReference type) { ConstantTable cTable = m_tableWriter.GetConstantTable (); ElementType et; if (type is TypeDefinition && (type as TypeDefinition).IsEnum) { Type t = hc.Constant.GetType (); if (t.IsEnum) t = Enum.GetUnderlyingType (t); et = GetCorrespondingType (string.Concat (t.Namespace, '.', t.Name)); } else et = GetCorrespondingType (type.FullName); ConstantRow cRow = m_rowWriter.CreateConstantRow ( et, hc.MetadataToken, m_mdWriter.AddBlob (EncodeConstant (et, hc.Constant))); cTable.Rows.Add (cRow); }