本文整理汇总了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);
//}
}
示例2: VerificationHelper
private void VerificationHelper(FieldBuilder field, object value, Type expected)
{
Assert.Throws(expected, () => { field.SetConstant(value); });
}
示例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");
}
}
}
示例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);
}
}