Class Extensions
- Namespace
- Ecng.ComponentModel
- Assembly
- Ecng.ComponentModel.dll
Provides a collection of extension methods for components, attributes, and debugging.
public static class Extensions
- Inheritance
-
Extensions
- Inherited Members
Methods
CanAutoLogin(ServerCredentials)
Determines if the server credentials contain sufficient data for auto login.
public static bool CanAutoLogin(this ServerCredentials credentials)
Parameters
credentials
ServerCredentialsThe server credentials.
Returns
- bool
True if auto login is possible; otherwise, false.
GetBasicProperties(object, int)
Retrieves basic properties (marked with BasicSettingAttribute) for the given instance, optionally retrieving nested properties recursively.
public static IEnumerable<(PropertyDescriptor prop, string path)> GetBasicProperties(this object instance, int maxDepth = 0)
Parameters
Returns
- IEnumerable<(PropertyDescriptor prop, string path)>
An enumerable of tuples containing the property descriptor and its path.
GetCategory(ICustomAttributeProvider, string)
Retrieves the category name for the given attribute provider using DisplayAttribute or CategoryAttribute.
public static string GetCategory(this ICustomAttributeProvider provider, string defaultValue = null)
Parameters
provider
ICustomAttributeProviderThe custom attribute provider.
defaultValue
stringThe default category if none is found.
Returns
- string
The category name.
GetDescription(ICustomAttributeProvider, string)
Retrieves the description for the given attribute provider using DisplayAttribute or DescriptionAttribute.
public static string GetDescription(this ICustomAttributeProvider provider, string defaultValue = null)
Parameters
provider
ICustomAttributeProviderThe custom attribute provider.
defaultValue
stringThe default description if none is found.
Returns
- string
The description string.
GetDescription<TEntity>(TEntity)
Retrieves the description for the entity from its DisplayAttribute.
public static string GetDescription<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- string
The description if set; otherwise, null.
Type Parameters
TEntity
The type of the attribute entity.
GetDisplayName(PropertyDescriptor, string)
Retrieves the display name for the specified property descriptor using DisplayAttribute or DisplayNameAttribute.
public static string GetDisplayName(this PropertyDescriptor pd, string defaultValue = null)
Parameters
pd
PropertyDescriptorThe property descriptor.
defaultValue
stringThe default value if no display name is found.
Returns
- string
The display name.
GetDisplayName(object)
Retrieves the display name of the given object. If the object is an Enum, its field display name is returned; otherwise, it uses custom attribute providers or type name.
public static string GetDisplayName(this object value)
Parameters
value
objectThe object for which to get the display name.
Returns
- string
The display name.
GetDisplayName(ICustomAttributeProvider, string)
Retrieves the display name for the given attribute provider using DisplayAttribute or DisplayNameAttribute.
public static string GetDisplayName(this ICustomAttributeProvider provider, string defaultValue = null)
Parameters
provider
ICustomAttributeProviderThe custom attribute provider.
defaultValue
stringThe default value if no display name is found.
Returns
- string
The display name.
GetDisplayName<TEntity>(TEntity)
Retrieves the display name for the entity from its DisplayAttribute.
public static string GetDisplayName<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- string
The display name if set; otherwise, null.
Type Parameters
TEntity
The type of the attribute entity.
GetDisplay<TEntity>(TEntity)
Retrieves the DisplayAttribute from the entity.
public static DisplayAttribute GetDisplay<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- DisplayAttribute
The DisplayAttribute if exists; otherwise, null.
Type Parameters
TEntity
The type of the attribute entity.
GetDocUrl(Type)
Retrieves the documentation URL for a type based on its DocAttribute.
public static string GetDocUrl(this Type type)
Parameters
type
TypeThe type to retrieve the doc URL for.
Returns
- string
The documentation URL if available; otherwise, null.
GetFieldDescription(object)
Retrieves the description for an enum field using the DisplayAttribute or DescriptionAttribute.
public static string GetFieldDescription(this object field)
Parameters
field
objectThe enum field.
Returns
- string
The field's description or an empty string if none is provided.
GetFieldDisplayName(object)
Retrieves the display name for an enum field using its custom attributes.
public static string GetFieldDisplayName(this object field)
Parameters
field
objectThe enum field.
Returns
- string
The display name associated with the field.
GetFieldIcon(object)
Retrieves the icon URI for an enum field based on its IconAttribute.
public static Uri GetFieldIcon(this object field)
Parameters
field
objectThe enum field.
Returns
- Uri
The icon URI if available; otherwise, null.
GetFilteredProperties(ICustomTypeDescriptor, Attribute[])
Retrieves the filtered property descriptors from a custom type descriptor based on the provided attributes.
public static PropertyDescriptorCollection GetFilteredProperties(this ICustomTypeDescriptor descriptor, Attribute[] attributes)
Parameters
descriptor
ICustomTypeDescriptorThe custom type descriptor.
attributes
Attribute[]The attributes to filter properties.
Returns
- PropertyDescriptorCollection
A filtered PropertyDescriptorCollection.
GetGroupName<TEntity>(TEntity)
Retrieves the group name for the entity from its DisplayAttribute.
public static string GetGroupName<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- string
The group name if set; otherwise, null.
Type Parameters
TEntity
The type of the attribute entity.
GetIconUrl(Type)
Retrieves the icon URL for a type based on its IconAttribute.
public static Uri GetIconUrl(this Type type)
Parameters
type
TypeThe type to retrieve the icon URL for.
Returns
- Uri
The icon URI if available; otherwise, null.
GetResourceUrl(string)
Retrieves the resource URL for the given resource name using the entry assembly.
public static Uri GetResourceUrl(this string resName)
Parameters
resName
stringThe resource name.
Returns
- Uri
The resource URI.
GetResourceUrl(string, Type)
Retrieves the resource URL for the given resource name using the specified type's assembly.
public static Uri GetResourceUrl(this string resName, Type type)
Parameters
Returns
- Uri
The resource URI.
GetValues(ItemsSourceAttribute)
Retrieves the items source items from an ItemsSourceAttribute instance.
public static IEnumerable<IItemsSourceItem> GetValues(this ItemsSourceAttribute attr)
Parameters
attr
ItemsSourceAttributeThe items source attribute.
Returns
- IEnumerable<IItemsSourceItem>
An enumerable of IItemsSourceItem.
IsBasic<TEntity>(TEntity)
Checks if the entity has a BasicSettingAttribute.
public static bool IsBasic<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- bool
True if the entity is marked as basic; otherwise, false.
Type Parameters
TEntity
The type of the attribute entity.
IsBrowsable<TEntity>(TEntity)
Checks if all BrowsableAttributes on the entity indicate it is browsable.
public static bool IsBrowsable<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- bool
True if the entity is browsable; otherwise, false.
Type Parameters
TEntity
The type of the attribute entity.
IsReadOnly<TEntity>(TEntity)
Checks if the entity is marked as read-only via a ReadOnlyAttribute.
public static bool IsReadOnly<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- bool
True if the entity is read-only; otherwise, false.
Type Parameters
TEntity
The type of the attribute entity.
IsValid<TEntity>(TEntity, object)
Validates the given value against all ValidationAttribute validators on the entity.
public static bool IsValid<TEntity>(this TEntity entity, object value) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
value
objectThe value to validate.
Returns
- bool
True if all validators deem the value valid; otherwise, false.
Type Parameters
TEntity
The type of the attribute entity.
IsWaiting(IDebugger)
Determines whether the debugger is currently waiting for input or output.
public static bool IsWaiting(this IDebugger debugger)
Parameters
debugger
IDebuggerThe debugger instance.
Returns
- bool
True if the debugger is waiting; otherwise, false.
SetAttribute<TEntity, TAttribute>(TEntity, bool, Func<TAttribute>)
Sets or removes an attribute of type TAttribute for the entity based on the provided value.
public static TEntity SetAttribute<TEntity, TAttribute>(this TEntity entity, bool value, Func<TAttribute> create) where TEntity : IAttributesEntity where TAttribute : Attribute
Parameters
entity
TEntityThe attribute entity.
value
boolTrue to add the attribute; false to remove it.
create
Func<TAttribute>A function to create the attribute instance.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
TAttribute
The type of attribute to set.
SetBasic<TEntity>(TEntity, bool)
Sets the BasicSettingAttribute for the entity.
public static TEntity SetBasic<TEntity>(this TEntity entity, bool isBasic = true) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
isBasic
boolTrue to mark as basic; otherwise, false.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetDisplay<TEntity>(TEntity, string, string, string, int)
Sets the DisplayAttribute for the entity with specified group, display name, description, and order.
public static TEntity SetDisplay<TEntity>(this TEntity entity, string groupName, string displayName, string description, int order) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
groupName
stringThe group/category name.
displayName
stringThe display name.
description
stringThe description text.
order
intThe display order.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetEditor<TEntity>(TEntity, Attribute)
Sets a custom editor attribute for the entity.
public static TEntity SetEditor<TEntity>(this TEntity entity, Attribute editor) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
editor
AttributeThe editor attribute to set.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetExpandable<TEntity>(TEntity, bool)
Sets the ExpandableObjectConverter attribute for the entity.
public static TEntity SetExpandable<TEntity>(this TEntity entity, bool expandable) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
expandable
boolTrue to set as expandable; otherwise, false.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetNonBrowsable<TEntity>(TEntity, bool)
Sets the BrowsableAttribute (non-browsable) for the entity.
public static TEntity SetNonBrowsable<TEntity>(this TEntity entity, bool nonBrowsable = true) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
nonBrowsable
boolTrue to mark as non-browsable; otherwise, false.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetReadOnly<TEntity>(TEntity, bool)
Sets the ReadOnlyAttribute for the entity.
public static TEntity SetReadOnly<TEntity>(this TEntity entity, bool readOnly = true) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
readOnly
boolTrue to mark as read-only; otherwise, false.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetRequired<TEntity>(TEntity)
Sets a RequiredAttribute as a validator for the entity.
public static TEntity SetRequired<TEntity>(this TEntity entity) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
SetValidator<TEntity>(TEntity, ValidationAttribute)
Adds a custom validation attribute as a validator for the entity.
public static TEntity SetValidator<TEntity>(this TEntity entity, ValidationAttribute validator) where TEntity : IAttributesEntity
Parameters
entity
TEntityThe attribute entity.
validator
ValidationAttributeThe validation attribute to add.
Returns
- TEntity
The updated attribute entity.
Type Parameters
TEntity
The type of the attribute entity.
ToN(Guid)
Converts the Guid to its 32-digit hexadecimal ("N") format.
public static string ToN(this Guid id)
Parameters
id
GuidThe Guid value.
Returns
- string
The "N" formatted string.
TryGetDefault(EventDescriptorCollection, Type)
Attempts to retrieve the default event descriptor from a collection based on the DefaultEventAttribute.
public static EventDescriptor TryGetDefault(this EventDescriptorCollection events, Type type)
Parameters
events
EventDescriptorCollectionThe event descriptor collection.
type
TypeThe type to check for default event.
Returns
- EventDescriptor
The default EventDescriptor if found; otherwise, null.
TryGetDefault(PropertyDescriptorCollection, Type)
Attempts to retrieve the default property descriptor from a collection based on the DefaultPropertyAttribute.
public static PropertyDescriptor TryGetDefault(this PropertyDescriptorCollection properties, Type type)
Parameters
properties
PropertyDescriptorCollectionThe property descriptor collection.
type
TypeThe type to check for default property.
Returns
- PropertyDescriptor
The default PropertyDescriptor if found; otherwise, null.
TryGetGuid(Type)
Attempts to retrieve the GUID associated with the specified control type using its GuidAttribute.
public static Guid? TryGetGuid(this Type controlType)
Parameters
controlType
TypeThe control type.
Returns
- Guid?
The GUID if available; otherwise, null.
Typed(PropertyDescriptorCollection)
Casts a PropertyDescriptorCollection to an enumerable of PropertyDescriptor.
public static IEnumerable<PropertyDescriptor> Typed(this PropertyDescriptorCollection col)
Parameters
col
PropertyDescriptorCollectionThe collection of property descriptors.
Returns
- IEnumerable<PropertyDescriptor>
An enumerable of PropertyDescriptor.