Export (0) Print
Expand All

FieldBuilder.SetConstant Method

Sets the default value of this field.

Namespace:  System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public void SetConstant(
	Object defaultValue
)

Parameters

defaultValue
Type: System.Object
The new default value for this field.

ExceptionCondition
InvalidOperationException

The containing type has been created using CreateType.

ArgumentException

The field is not one of the supported types.

-or-

The type of defaultValue does not match the type of the field.

-or-

The field is of type Object or other reference type, defaultValue is not null, and the value cannot be assigned to the reference type.

defaultValue is restricted to the following types: Boolean, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, UInt64, Single, Double, DateTime, Char, String, and Enum. If the field type is a reference type, CLS compliance requires defaultValue to be null. However, starting with the .NET Framework version 4, defaultValue can be non-null for a reference type as long as the value can be assigned to that reference type. For example, an Int32 value can be assigned to a field of type Object. This is not CLS-compliant, but it can be useful in interop scenarios.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft