AFElement.ConvertTo Method
- Last UpdatedJan 12, 2026
- 3 minute read
- PI System
- AF SDK 3.2.0
- Developer
Converts the current object to the specified type of an AFElement.
Namespace: OSIsoft.AF.Asset
Assembly: OSIsoft.AFSDK (in OSIsoft.AFSDK.dll) Version: 3.2.0.7
Syntax
public AFElement ConvertTo( Type destinationType )
Public Function ConvertTo ( destinationType As Type ) As AFElement Dim instance As AFElement Dim destinationType As Type Dim returnValue As AFElement returnValue = instance.ConvertTo(destinationType)
public: AFElement^ ConvertTo( Type^ destinationType )
member ConvertTo : destinationType : Type -> AFElement
Parameters
- destinationType
- Type: SystemType
The Type to convert the current object to. The destination type must be AFElement or inherit from AFElement.
Return Value
Type: AFElementAn AFElement that represents the converted type. See CreateInstance(Type, BindingFlags, Binder, Object, CultureInfo) for a list of additional exceptions that might be thrown by this method.
Exceptions
| Exception | Condition |
|---|---|
| ArgumentNullException | The destinationType is . |
| ArgumentException | The destinationType is not a RuntimeType. |
| NotSupportedException | The conversion could not be performed. |
Remarks
The most common type of conversion is to convert from an element to a more specific type of element like AFModel. When converting from a more specific type of an element to an AFElement, information that is specific to that type will be lost. For example, converting an AFModel to back to an AFElement will lose any AFConnections and AFLayers that it has defined.
Before returning, the CheckIn method will automatically be called to save the new type of the object to the server. Therefore, this operation cannot be undone by calling UndoCheckOut(Boolean).
| The element must be saved to the server before converting. Also, the element being converted cannot have multiple versions. |
Examples
// This example demonstrates how to convert an element to a model. // Get the Database PISystems myPISystems = new PISystems(); AFDatabase myDB = myPISystems.DefaultPISystem.Databases.DefaultDatabase; // Create an Element AFElement myElement = myDB.Elements.Add("MyElement"); myElement.ApplyChanges(); // Convert the Element to a Model AFModel myModel = (AFModel)myElement.ConvertTo(typeof(AFModel)); // Once an element has been converted, it will always be a model. Output will be "AFModel" myElement = myDB.Elements["MyElement"]; Console.WriteLine("MyElement is of type: {0}", myElement.GetType());