// Copyright Keysight Technologies 2012-2019 // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, you can obtain one at http://mozilla.org/MPL/2.0/. using System; using System.Collections.Generic; using System.Linq; namespace OpenTap { /// /// An immutable/constant object that is a property on a Resource. When deserializing references to objects of this type, /// makes sure that all references refer to the same unique instance on the specific instrument. /// public interface IConstResourceProperty { /// /// TDevice/resource to which this object belongs. /// /// /// All devices should be marked with the . /// [System.Xml.Serialization.XmlIgnore] IResource Device { get; } /// /// Name of this object (should be unique among objects of the same type on the same device/resource). /// string Name { get; } } /// /// Helper and extension methods for IConstResourceProperty. /// public static class IConstResourcePropertyHelpers { /// /// Returns all IConstResourceProperty instances of a specific type defined on this resource. /// public static IEnumerable GetConstProperties(this IResource res) where T : IConstResourceProperty { if(res == null) throw new ArgumentNullException("res"); return res.GetConstProperties().OfType(); } /// /// Returns all IConstResourceProperty instances on a resource. /// /// /// public static IEnumerable GetConstProperties(this IResource res) => GetConstPropertiesImpl(res).ToArray(); static IEnumerable GetConstPropertiesImpl(IResource res) { foreach (var prop in TypeData.GetTypeData(res).GetMembers()) { if (prop.Readable == false) continue; if (prop.TypeDescriptor.DescendsTo(typeof(IConstResourceProperty))) { var value = (IConstResourceProperty)prop.GetValue(res); if (value == null) continue; yield return value; } if (prop.TypeDescriptor.DescendsTo(typeof(IEnumerable))) { var points = (IEnumerable)prop.GetValue(res); if (points == null) continue; foreach (var pt in points) { if (pt == null) continue; yield return pt; } } } } } }