Description
The IsInitOnly
method is an extension method for the System.Reflection.PropertyInfo
class. It determines whether a given property is marked as init
-only. An init
-only property can only be set during object initialization, making it immutable after the object is constructed.
Usage
To use the IsInitOnly
method, you need to pass a PropertyInfo
object representing the property you want to check. The method will return true
if the property is init
-only, otherwise it will return false
.
Example
// Example usage of IsInitOnly method
using System;
using System.Reflection;
public class Example
{
public init string InitOnlyProperty { get; init; }
public string RegularProperty { get; set; }
public static void Main()
{
PropertyInfo initOnlyProp = typeof(Example).GetProperty("InitOnlyProperty");
PropertyInfo regularProp = typeof(Example).GetProperty("RegularProperty");
bool isInitOnly = SandboxSystemExtensions.IsInitOnly(initOnlyProp);
bool isRegularInitOnly = SandboxSystemExtensions.IsInitOnly(regularProp);
Console.WriteLine($"InitOnlyProperty is init-only: {isInitOnly}"); // Output: true
Console.WriteLine($"RegularProperty is init-only: {isRegularInitOnly}"); // Output: false
}
}