public SetConstant ( object defaultValue ) : void | ||
defaultValue | object | |
return | void |
/// <summary>Defines the named static field in an enumeration type with the specified constant value.</summary> /// <returns>The defined field.</returns> /// <param name="literalName">The name of the static field. </param> /// <param name="literalValue">The constant value of the literal. </param> public FieldBuilder DefineLiteral(string literalName, object literalValue) { FieldBuilder fieldBuilder = this._tb.DefineField(literalName, this, FieldAttributes.FamANDAssem | FieldAttributes.Family | FieldAttributes.Static | FieldAttributes.Literal); fieldBuilder.SetConstant(literalValue); return(fieldBuilder); }
public FieldBuilder DefineLiteral(string literalName, object literalValue) { FieldBuilder builder = this.m_typeBuilder.DefineField(literalName, this, FieldAttributes.Literal | FieldAttributes.Static | FieldAttributes.Public); builder.SetConstant(literalValue); return(builder); }
/// <summary>用指定的常数值定义枚举类型中已命名的静态字段。</summary> /// <returns>定义的字段。</returns> /// <param name="literalName">静态字段的名称。</param> /// <param name="literalValue">Literal 的常数值。</param> public FieldBuilder DefineLiteral(string literalName, object literalValue) { FieldBuilder fieldBuilder = this.m_typeBuilder.DefineField(literalName, (Type)this, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal); object defaultValue = literalValue; fieldBuilder.SetConstant(defaultValue); return(fieldBuilder); }
public FieldBuilder DefineLiteral(string literalName, object literalValue) { Type fieldType = this; FieldBuilder fieldBuilder = _tb.DefineField(literalName, fieldType, (FieldAttributes.Literal | (FieldAttributes.Static | FieldAttributes.Public))); fieldBuilder.SetConstant(literalValue); return(fieldBuilder); }
// Define literal for enum public FieldBuilder DefineLiteral(string literalName, object?literalValue) { // Define the underlying field for the enum. It will be a non-static, private field with special name bit set. FieldBuilder fieldBuilder = m_typeBuilder.DefineField( literalName, this, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal); fieldBuilder.SetConstant(literalValue); return(fieldBuilder); }
// Define literal for enum public FieldBuilder DefineLiteral(String literalName, Object literalValue) { BCLDebug.Log("DYNIL", "## DYNIL LOGGING: EnumBuilder.DefineLiteral( " + literalName + " )"); // Define the underlying field for the enum. It will be a non-static, private field with special name bit set. FieldBuilder fieldBuilder = m_typeBuilder.DefineField( literalName, this, FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal); fieldBuilder.SetConstant(literalValue); return(fieldBuilder); }
public FieldBuilder DefineLiteral(string literalName, object literalValue) { #if NET_2_0 Type fieldType = this; #else Type fieldType = _underlyingType; #endif FieldBuilder fieldBuilder = _tb.DefineField(literalName, fieldType, (FieldAttributes.Literal | (FieldAttributes.Static | FieldAttributes.Public))); fieldBuilder.SetConstant(literalValue); return(fieldBuilder); }
// 构造字段 void BuildField() { _realProxyField = _typeBuilder.DefineField("_realProxy", _realProxyType, FieldAttributes.Private); _realProxyField.SetConstant(null); //if ( _baseType!=null ) //{ // _dbContextTransaction = _typeBuilder.DefineField("_dbContextTransaction", typeof(System.Data.Entity.DbContextTransaction), FieldAttributes.Private); // _dbContextTransaction.SetConstant(null); //} }
private void VerificationHelper(FieldBuilder field, object value, Type expected) { Assert.Throws(expected, () => { field.SetConstant(value); }); }
void BuildField() { _realProxyField = _typeBuilder.DefineField("_realProxy", _realProxyType, FieldAttributes.Private); _realProxyField.SetConstant(null); }
/// <summary> /// Provide implementation for setting default value /// </summary> private static void SetDefaultValueInternal(IntPtr ipVariant, Type type, ParameterBuilder paramBuilder, FieldBuilder fieldBuilder, short typeVt) { if (type == null) throw new ArgumentNullException(nameof(type)); // Use the element type for normalization if the type is a ByRef while (type.IsByRef) type = type.GetElementType(); short defaultValueVt = Marshal.ReadInt16(ipVariant); object defaultValue = GetNormalizedDefaultValueFromVariant(ipVariant, type, defaultValueVt, typeVt); if (type.IsEnum && (defaultValueVt == (short)VarEnum.VT_I4 || defaultValueVt == (short)VarEnum.VT_UI4)) { // 1) I can't dynamically create the enum in a ReflectionOnlyContext // 2) In pre-4.0, SetConstant(1) won't work for enum types. // After talking with team members we decided that this is a minor functionality that // we can live without in pre-4.0 // return; } // Currently ParameterBuilder.SetConstant doesn't emit the custom attributes so certain types of constants // so we need to do that by ourselves CustomAttributeBuilder builder = null; if (type == typeof(Decimal)) { DECIMAL realDecimal; IntPtr pDecimal = IntPtr.Zero; // Unfortunate we cannot directly access the fields in the decimal struct and we need to rely on the fact // that the internal representation of decimal & DECIMAL are the same, which will most likely remain true in the future // because CLR internally does the same thing Debug.Assert(sizeof(decimal) == Marshal.SizeOf(typeof(DECIMAL))); try { pDecimal = Marshal.AllocCoTaskMem(sizeof(decimal)); // We convert it to unmanaged then back to managed to avoid unsafe code, so that we won't crash immediately in partial trust Marshal.StructureToPtr(defaultValue, pDecimal, false); realDecimal = (DECIMAL)Marshal.PtrToStructure(pDecimal, typeof(DECIMAL)); } finally { if (pDecimal != IntPtr.Zero) Marshal.FreeCoTaskMem(pDecimal); } builder = CustomAttributeHelper.GetBuilderForDecimalConstant( realDecimal.scale, realDecimal.sign, realDecimal.hi32, realDecimal.mid32, realDecimal.low32); } else if (type == typeof(DateTime) && defaultValueVt == (short)VarEnum.VT_DATE) { builder = CustomAttributeHelper.GetBuilderForDateTimeConstant(((DateTime)defaultValue).Ticks); } else if (defaultValueVt == (short)VarEnum.VT_UNKNOWN) { // Currently ParameterBuilder.SetConstant doesn't emit the IUnknownConstantAttribute // for IUnknown so we need to do that by ourselves builder = CustomAttributeHelper.GetBuilderForIUnknownConstant(); } else if (defaultValueVt == (short)VarEnum.VT_DISPATCH) { // Currently ParameterBuilder.SetConstant doesn't emit the IDispatchConstantAttribute // for IDispatch so we need to do that by ourselves builder = CustomAttributeHelper.GetBuilderForIDispatchConstant(); } if (builder != null) { if (paramBuilder != null) paramBuilder.SetCustomAttribute(builder); if (fieldBuilder != null) fieldBuilder.SetCustomAttribute(builder); } else { try { if (paramBuilder != null) paramBuilder.SetConstant(defaultValue); if (fieldBuilder != null) fieldBuilder.SetConstant(defaultValue); } catch (Exception) { // Debug.Assert(type.IsEnum, "We should avoid failing for non-Enum default values"); } } }
protected override void PreBuild() { if (Info != null) { return; } var cont = CurrentContainer; Builder = cont.CreateField(Name, DataType.GainType(), Attributes); Info = Builder; if (DefaultValue != null) { Builder.SetConstant(DefaultValue); } }