Class LogixElement
- Namespace
- L5Sharp
- Assembly
- L5Sharp.dll
A base class for all types that can be serialized and deserialized from a L5X file. This abstract class enforces the ILogixSerializable interface and a constructor taking a XElement for initialization of and underlying element object. Deriving classes will have access to the underlying element and methods for easily getting and setting data.
public abstract class LogixElement : ILogixSerializable
- Inheritance
-
LogixElement
- Implements
- Derived
- Inherited Members
Constructors
LogixElement()
Creates a new default LogixElement initialized with an XElement having the L5XType name of the element.
protected LogixElement()
LogixElement(XElement)
Initializes a new LogixElement with the provided XElement
protected LogixElement(XElement element)
Parameters
Exceptions
- ArgumentNullException
element
is null.
Fields
Element
The underlying XElement representing the backing data for the entity. Use this object to store and retrieve data for the component. This property is the basis for serialization and deserialization of L5X data.
protected readonly XElement Element
Field Value
Properties
IsAttached
An indication as to whether this element is attached to a L5X document.
public bool IsAttached { get; }
Property Value
- bool
true
if this is an attached element; Otherwise,false
.
Remarks
This simply looks to see if the element has a ancestor with the root RSLogix5000Content element or not. If so we will assume this element is attached to an overall L5X document.
Methods
Clone()
Returns a new deep cloned instance of the current type.
public LogixElement Clone()
Returns
- LogixElement
A new LogixElement type with the same property values.
Remarks
This method will simply deserialize a new instance using the current underlying element data.
Exceptions
- InvalidOperationException
The object being cloned does not have a constructor accepting a single XElement argument.
Clone<TElement>()
Returns a new deep cloned instance as the specified LogixElement type.
public TElement Clone<TElement>() where TElement : LogixElement
Returns
- TElement
A new instance of the specified element type with the same property values.
Type Parameters
TElement
The LogixElement type to cast to.
Remarks
This method will simply deserialize a new instance using the current underlying element data.
Exceptions
- InvalidOperationException
The object being cloned does not have a constructor accepting a single XElement argument.
- InvalidCastException
The deserialized type can not be cast to the specified generic type parameter.
GetComplex<T>(string?)
Gets a immediate child element of the specified member name if it exists and deserializes it as the
specific generic type parameter. If the element does not exist, returns default
.
protected T? GetComplex<T>(string? name = null) where T : LogixElement
Parameters
name
stringThe name of the child element.
Returns
- T
If found, the value of child element deserialized as the generic type parameter. If not found, returns
default
.
Type Parameters
T
The return type of the element.
Remarks
This method makes getting/setting data on Element as concise as possible for derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
GetContainer<TChild>(string?)
Gets a child LogixContainer<TElement> with the specified element name, representing the root of a collection of contained elements.
protected LogixContainer<TChild> GetContainer<TChild>(string? name = null) where TChild : LogixElement
Parameters
name
stringThe name of the child container collection (e.g. Members).
Returns
- LogixContainer<TChild>
A LogixContainer<TElement> containing all the child elements of the specified type.
Type Parameters
TChild
The child element type.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
Exceptions
- InvalidOperationException
A child element with
name
does not exist.
GetDateTime(string?, string?)
Gets the date/time value of the specified attribute name from the current element if it exists. If the attribute does not exist, returns default value.
protected DateTime? GetDateTime(string? format = null, string? name = null)
Parameters
format
stringThe format of the date time. If not provided will default to 'ddd MMM d HH:mm:ss yyyy' which is a typical L5X date time format.
name
stringThe name of the date time attribute.
Returns
Remarks
This is a specialized helper since the date time formats are different for different component properties, we need to allow that to be specified.
GetProperty<T>(string?)
Gets the value of the specified child element parsed as the specified generic type parameter if it exists.
If the element does not exist, returns default
value of the generic type parameter.
protected T? GetProperty<T>(string? name = null)
Parameters
name
stringThe name of the child element.
Returns
- T
If found, the value of child element parsed as the generic type parameter. If not found, returns
default
.
Type Parameters
T
The return type of the value.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
GetRequiredValue<T>(string?)
Gets the value of the specified attribute name from the element parsed as the specified generic type parameter. If the attribute does not exist, throw a InvalidOperationException.
protected T GetRequiredValue<T>(string? name = null)
Parameters
name
stringThe name of the attribute.
Returns
- T
The value of attribute parsed as the generic type parameter.
Type Parameters
T
The return type of the value.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
Exceptions
- InvalidOperationException
No attribute with the provided name was found on Element.
GetValue<T>(Func<XElement, XElement?>, string?)
Gets the value of the selected attribute parsed as the specified generic type parameter if it exists.
If the attribute does not exist, returns default
value of the generic type parameter.
protected T? GetValue<T>(Func<XElement, XElement?> selector, string? name = null)
Parameters
selector
Func<XElement, XElement>A selection delegate that allows custom selection of a element relative to Element. Use this to reach down the element hierarchy for nested values.
name
stringThe name of the attribute.
Returns
- T
If found, the value of attribute parsed as the generic type parameter. If not found, returns
default
.
Type Parameters
T
The return type of the value.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
GetValue<T>(string?)
Gets the value of the specified attribute name from the element parsed as the specified generic type parameter if it exists.
If the attribute does not exist, returns default
value of the generic type parameter.
protected T? GetValue<T>(string? name = null)
Parameters
name
stringThe name of the attribute.
Returns
- T
If found, the value of attribute parsed as the generic type parameter. If not found, returns
default
.
Type Parameters
T
The return type of the value.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
GetValue<T>(XName, string?)
Gets the value of a child element attribute parsed as the specified generic type parameter if it exists.
If the attribute does not exist, returns default
value of the generic type parameter.
protected T? GetValue<T>(XName child, string? name = null)
Parameters
child
XNameThe name of the child element containing the attribute value to retrieve.
name
stringThe name of the attribute.
Returns
- T
If found, the value of attribute parsed as the generic type parameter. If not found, returns
default
.
Type Parameters
T
The return type of the value.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
L5X()
Returns a LogixContent instance wrapping the current element's root L5X element if it exists.
public LogixContent? L5X()
Returns
- LogixContent
If the current element is attached to a L5X document (i.e. has a root content element), then a new LogixContent instance wrapping the root; Otherwise,
null
.
Remarks
This allows attached logix elements to reach up to the content file in order to traverse or retrieve other elements in the L5X. This is helpful for other extensions that need rely on the L5X to perform functions.
Serialize()
Returns the underlying XElement for the LogixElement.
public virtual XElement Serialize()
Returns
SetComplex<T>(T?, string?)
Sets the complex type object of a child element, adds a child element, or removes a child element.
protected void SetComplex<T>(T? value, string? name = null) where T : ILogixSerializable
Parameters
value
TThe complex type to assign to the child element. The child element is removed if the value is null. Otherwise, the value is serialized and added as a child element to the current type's element.
name
stringThe name of the element to set.
Type Parameters
T
The value type.
Remarks
This method it only available to make getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
SetContainer<TChild>(LogixContainer<TChild>?, string?)
Sets the value of a child container, adds a child container, or removes a child container.
protected void SetContainer<TChild>(LogixContainer<TChild>? value, string? name = null) where TChild : LogixElement
Parameters
value
LogixContainer<TChild>The LogixContainer<TElement> value to set. The child container is removed if the value is null. Otherwise, the value is serialized and added (or replaces the existing) to underlying parent element.
name
stringThe name of the child container collection (e.g. Members).
Type Parameters
TChild
The container type parameter.
Remarks
This method it only available to make getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
SetDateTime(DateTime?, string?, string?)
Sets the date/time value of a attribute, adds a attribute, or removes a attribute if null.
protected void SetDateTime(DateTime? value, string? format = null, string? name = null)
Parameters
value
DateTime?The value to set.
format
stringThe format of the date time. If not provided will default to 'ddd MMM d HH:mm:ss yyyy' which is a typical L5X date time format.
name
stringThe name of the date time attribute.
Remarks
This is a specialized helper since the date time formats are different for different component properties, we should allow that to be specified.
SetProperty<T>(T, string?)
Sets the value of a child element, adds a child element, or removes a child element.
protected void SetProperty<T>(T value, string? name = null)
Parameters
value
TThe value to assign to the child element. The child element is removed if the value is null. Otherwise, the value is converted to its string representation, wrapped in a XCData object, and assigned to the Value property of the child element.
name
stringThe name of the element to set.
Type Parameters
T
The value type.
Remarks
This method it only available to make getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
SetRequiredValue<T>(T, string?)
Sets or adds the value of an attribute on the underlying element.
protected void SetRequiredValue<T>(T value, string? name = null)
Parameters
value
TThe value to assign to the attribute. If null, an exception will be thrown.
name
stringThe name of the attribute to set.
Type Parameters
T
The value type.
Remarks
This method it only available to make getting/setting data on Element as concise
as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving
classes don't have to specify the property name (assuming its the name matches the underlying element property).
This method will throw an exception if the value
is null.
Exceptions
- ArgumentNullException
value
is null.
SetValue<T>(T?, Func<XElement, XElement?>, string?)
Sets the value of an attribute, adds an attribute, or removes an attribute for a element obtained using the provided selector delegate.
protected void SetValue<T>(T? value, Func<XElement, XElement?> selector, string? name = null)
Parameters
value
TThe value to assign to the attribute. The attribute is removed if the value is null. Otherwise, the value is converted to its string representation and assigned to the Value property of the attribute.
selector
Func<XElement, XElement>A selection delegate that allows custom selection of a element relative to Element. Use this to reach down the element hierarchy for nested values.
name
stringThe name of the attribute to set.
Type Parameters
T
The value type.
Remarks
This method helps make getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
SetValue<T>(T?, string?)
Sets the value of an attribute, adds an attribute, or removes an attribute.
protected void SetValue<T>(T? value, string? name = null)
Parameters
value
TThe value to assign to the attribute. The attribute is removed if the value is null. Otherwise, the value is converted to its string representation and assigned to the Value property of the attribute.
name
stringThe name of the attribute to set.
Type Parameters
T
The value type.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).
SetValue<T>(T?, XName, string?)
Sets the value of an attribute, adds an attribute, or removes an attribute for a nested element specified by provided element name.
protected void SetValue<T>(T? value, XName child, string? name = null)
Parameters
value
TThe value to assign to the attribute. The attribute is removed if the value is null. Otherwise, the value is converted to its string representation and assigned to the Value property of the attribute.
child
XNameThe name of the child XElement for which to set the attribute. If the element does not exist and attribute is not null, will create the element and add to the parent Element.
name
stringThe name of the attribute to set.
Type Parameters
T
The value type.
Remarks
This method makes getting/setting data on Element as concise as possible from derived classes. This method uses the CallerMemberNameAttribute so the deriving classes don't have to specify the property name (assuming its the name matches the underlying element property).