FieldBuilder.SetConstant Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Sets the default value of this field.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Sub SetConstant ( _
    defaultValue As Object _
)
[SecuritySafeCriticalAttribute]
public void SetConstant(
    Object defaultValue
)

Parameters

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

Exceptions

Exception Condition
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 nulla null reference (Nothing in Visual Basic), and the value cannot be assigned to the reference type.

Remarks

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 nulla null reference (Nothing in Visual Basic). However, starting with Silverlight 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.

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.