//****************************************************************************************************** // ValueExpressionAttributeBase.cs - Gbtc // // Copyright © 2017, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this // file except in compliance with the License. You may obtain a copy of the License at: // // http://opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 04/07/2017 - J. Ritchie Carroll // Generated original version of source code. // //****************************************************************************************************** using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; using ExpressionEvaluator; namespace GSF.ComponentModel { /// /// Represents a base attribute class for C# expressions that when evaluated will specify a new value for a property. /// [AttributeUsage(AttributeTargets.Property)] public abstract class ValueExpressionAttributeBase : Attribute, IValueExpressionAttribute { #region [ Constructors ] /// /// Creates a new /// /// C# expression that will evaluate to the desired value. /// Parameter cannot be null. protected ValueExpressionAttributeBase(string expression) { if (string.IsNullOrWhiteSpace(expression)) throw new ArgumentNullException(nameof(expression)); Expression = expression; } #endregion #region [ Properties ] /// /// Gets C# expression that will evaluate to the desired value. /// /// /// Use the method to get a per-property /// based value for use with the . Use /// to automatically /// replace any this keywords with Instance so as to properly /// reference the modeled . /// public string Expression { get; } /// /// Gets or sets value that determines if value should be cached after first evaluation. /// Defaults to false. /// public bool Cached { get; set; } /// /// Gets or sets the numeric evaluation order for this expression. Defaults to zero. /// /// /// /// This is useful for providing an order of operations to evaluations of /// attributes where one expression may /// be dependent on another. Note that properties are normally evaluated in the order /// in which they are defined in the class, but this is not guaranteed, using this /// attribute allows the order of evaluation to be changed. /// /// /// When no is specified, the sort order for a property /// will be zero. Properties will be ordered numerically based on this value. /// /// /// For any value that references the this keyword, /// a positive evaluation order will be required. /// /// /// See . /// /// public int EvaluationOrder { get; set; } /// /// Gets or sets any to use if the attribute /// needs to be pre-parsed. /// [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "TypeRegistry needs to be assigned when needed - default value is null")] public TypeRegistry TypeRegistry { get; set; } #endregion #region [ Methods ] /// /// Gets the based value used to update a modeled property. /// /// Property from which attribute was derived. /// Expression based on source property. /// /// /// This function allows derived attribute implementations to adjust the expression based /// on property information, e.g., property type. /// /// /// The property update value is typically used to assign expression values to a modeled type. /// /// public virtual string GetPropertyUpdateValue(PropertyInfo property) => Expression; /// /// Gets the modeled property based value used to update the . /// /// Property from which attribute was derived. /// Assignment expression based on source property. /// /// /// This function allows derived attribute implementations to adjust the update value /// based on property information, e.g., property type. /// /// /// The expression update value is typically used to assign modeled property values back /// to expressions allowing synchronization of a model with an external source, e.g., a /// user interface element. /// /// public virtual string GetExpressionUpdateValue(PropertyInfo property) => $"Instance.{property.Name}"; #endregion } }