# Class Extensions OptimaJet.Workflow.Core.Extensions Provides extension methods used by core workflow engine components. Namespace: [OptimaJet.Workflow.Core](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/) ```csharp public static class Extensions ``` ## Methods `public static XElement SingleOrDefault(this XElement element, string name)` Gets the current [XElement](https://learn.microsoft.com/dotnet/api/system.xml.linq.xelement) when its name matches; otherwise, gets the only direct child with the requested name. ### Parameters | Name | Type | Description | | --- | --- | --- | | `element` | `XElement` | The source XML element. | | `name` | `String` | The XML name to match. | ### Returns | Type | Description | | --- | --- | | `XElement` | The current element when its name equals `name`, the only matching direct child element with that name, or [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) when none is found. | ### Exceptions | Type | Description | | --- | --- | | `InvalidOperationException` | More than one direct child has the requested name. | `public static Type ToNullableType(this Type type)` Gets a nullable equivalent for value types. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The source type. | ### Returns | Type | Description | | --- | --- | | `Type` | The corresponding [Nullable](https://learn.microsoft.com/dotnet/api/system.nullable-1) type for a value type, the original nullable type when `type` is already nullable, or the original type for a reference type. | `public static bool IsNullable(this Type type)` Determines whether a type is a nullable value type. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The type to inspect. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when `type` is constructed from [Nullable](https://learn.microsoft.com/dotnet/api/system.nullable-1); otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | `public static Type GetUnderlyingType(this Type type)` Gets the non-nullable underlying type for a nullable type. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The type to inspect. | ### Returns | Type | Description | | --- | --- | | `Type` | The underlying type argument for [Nullable](https://learn.microsoft.com/dotnet/api/system.nullable-1) values, or the original type when the value is not nullable. | `public static object GetDefaultValue(this Type type)` Gets the default value for a type. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The type whose default value to create. | ### Returns | Type | Description | | --- | --- | | `Object` | The zero-initialized value for value types, or [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) for reference types. | `public static bool IsNumeric(this Type type)` Determines whether the type is one of the numeric types recognized by the engine. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The type to check. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when `type` is exactly one of the supported numeric types; otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | `public static string ToLowerCaseString(this bool value)` Returns the [bool](https://learn.microsoft.com/dotnet/api/system.boolean) value as lower-case text. ### Parameters | Name | Type | Description | | --- | --- | --- | | `value` | `Boolean` | The value to convert. | ### Returns | Type | Description | | --- | --- | | `String` | `true` as `true` and `false` as `false`. | `public static bool ExtendedEquals(this object value, object valueToCompare)` Compares two values with an extended rule for enumerable types. ### Parameters | Name | Type | Description | | --- | --- | --- | | `value` | `Object` | The value to compare. | | `valueToCompare` | `Object` | The value to compare against. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when the values are equal by this method's comparison rules; otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | ### Remarks When both values are non-null, have the same runtime type, and implement [IEnumerable](https://learn.microsoft.com/dotnet/api/system.collections.ienumerable), comparison is performed element-by-element by index. `public static string ToFormattedString(this List value, bool quotate)` Converts a list into a delimited string, using a comma as the separator. ### Parameters | Name | Type | Description | | --- | --- | --- | | `value` | `List` | The list to serialize. | | `quotate` | `Boolean` | Whether each item should be wrapped in single quotes. | ### Returns | Type | Description | | --- | --- | | `String` | The formatted string representation. | `public static string ToFormattedString(this List value, string delimeter, bool quotate)` Converts a list into a delimited string. ### Parameters | Name | Type | Description | | --- | --- | --- | | `value` | `List` | The list to serialize. | | `delimeter` | `String` | The delimiter to place between list items. | | `quotate` | `Boolean` | Whether each item should be wrapped in single quotes. | ### Returns | Type | Description | | --- | --- | | `String` | The formatted string representation. | `public static bool TryGetValueIgnoreCase(this IDictionary dictionary, string key, out TV value)` Tries to get a value from a dictionary using a case-insensitive key match. ### Parameters | Name | Type | Description | | --- | --- | --- | | `dictionary` | `IDictionary` | The dictionary to query. | | `key` | `String` | The key to match in a case-insensitive manner. | | `value` | `{TV}` | When the method returns [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool), contains the matching value; otherwise the default value. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when exactly one key matches `key` ignoring case; otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | `public static dynamic ToDynamic(this Dictionary dictionary)` Creates a dynamic object from a string/object dictionary. ### Parameters | Name | Type | Description | | --- | --- | --- | | `dictionary` | `Dictionary` | The source dictionary. | ### Returns | Type | Description | | --- | --- | | `dynamic` | An [ExpandoObject](https://learn.microsoft.com/dotnet/api/system.dynamic.expandoobject) containing all dictionary entries. | `public static string ToValidCSharpIdentifierName(this string originalString)` Converts non-empty text to an identifier-compatible string by replacing unsupported characters and adding a leading underscore when required. ### Parameters | Name | Type | Description | | --- | --- | --- | | `originalString` | `String` | The non-null, non-empty source text. | ### Returns | Type | Description | | --- | --- | | `String` | A sanitized identifier-compatible string. C# keywords are not escaped. | ### Exceptions | Type | Description | | --- | --- | | `ArgumentNullException` | `originalString` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). | | `ArgumentOutOfRangeException` | `originalString` is empty. | `public static T? FirstOrNull(this IEnumerable items, Func predicate) where T : struct` Returns the first element that matches a predicate, or [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null) when no elements match. ### Parameters | Name | Type | Description | | --- | --- | --- | | `items` | `IEnumerable` | The sequence to search. | | `predicate` | `Func` | The predicate used to test each element. | ### Returns | Type | Description | | --- | --- | | `Nullable` | A nullable `T` value for the first matching element, or [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). | ### Exceptions | Type | Description | | --- | --- | | `ArgumentNullException` | Thrown when `items` or `predicate` is [null](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/null). | `public static bool IsSimpleDynamicType(this Type type)` Determines whether a type is considered a simple type for dynamic bindings. ### Parameters | Name | Type | Description | | --- | --- | --- | | `type` | `Type` | The type to inspect. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when the type is a primitive type or one of the supported additional simple types; otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | `public static T CastAs(this object obj)` Casts an object to the requested type when the runtime type is assignable. ### Parameters | Name | Type | Description | | --- | --- | --- | | `obj` | `Object` | The source object. | ### Returns | Type | Description | | --- | --- | | `{T}` | The object cast to `T` when valid, otherwise the default value of `T`. | `public static bool TryCast(this object obj, out T newValue)` Tries to cast an object to a requested type. ### Parameters | Name | Type | Description | | --- | --- | --- | | `obj` | `Object` | The source object. | | `newValue` | `{T}` | When the method returns [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool), the casted value; otherwise default. | ### Returns | Type | Description | | --- | --- | | `Boolean` | [true](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool) when the cast succeeds; otherwise [false](https://learn.microsoft.com/dotnet/csharp/language-reference/builtin-types/bool). | [Previous BaseTemplate](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/BaseTemplate/) [Next FormBase](https://api.workflowengine.io/core/OptimaJet.Workflow.Core/FormBase/)