当前位置: 首页>>代码示例>>C#>>正文


C# FieldBuilder.SetConstant方法代码示例

本文整理汇总了C#中System.Reflection.Emit.FieldBuilder.SetConstant方法的典型用法代码示例。如果您正苦于以下问题:C# FieldBuilder.SetConstant方法的具体用法?C# FieldBuilder.SetConstant怎么用?C# FieldBuilder.SetConstant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.FieldBuilder的用法示例。


在下文中一共展示了FieldBuilder.SetConstant方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BuildField

 // 构造字段
 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);
     //}
 }
开发者ID:493409332,项目名称:MT.Easyui,代码行数:11,代码来源:DynamicProxyGenerator.cs

示例2: VerificationHelper

 private void VerificationHelper(FieldBuilder field, object value, Type expected)
 {
     Assert.Throws(expected, () => { field.SetConstant(value); });
 }
开发者ID:charygao,项目名称:corefx,代码行数:4,代码来源:FieldBuilderSetConstant.cs

示例3: SetDefaultValueInternal

        /// <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");
                }
            }
        }
开发者ID:dbremner,项目名称:clrinterop,代码行数:95,代码来源:ConvCommon.cs

示例4: PreBuild

 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);
     }
 }
开发者ID:B-head,项目名称:Dreit-prototype,代码行数:14,代码来源:FieldStructure.cs


注:本文中的System.Reflection.Emit.FieldBuilder.SetConstant方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。