chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//            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
{
    /// <summary>
    /// An immutable/constant object that is a property on a Resource. When deserializing references to objects of this type, 
    /// <see cref="TapSerializer"/> makes sure that all references refer to the same unique instance on the specific instrument.
    /// </summary>
    public interface IConstResourceProperty
    {
        /// <summary>
        /// TDevice/resource to which this object belongs.  
        /// </summary>
        /// <remarks>
        /// All devices should be marked with the <see cref="System.Xml.Serialization.XmlIgnoreAttribute"/>.
        /// </remarks>
        [System.Xml.Serialization.XmlIgnore]
        IResource Device { get; }
        /// <summary>
        /// Name of this object (should be unique among objects of the same type on the same device/resource).  
        /// </summary>
        string Name { get; }
    }
 
    /// <summary>
    /// Helper and extension methods for IConstResourceProperty.
    /// </summary>
    public static class IConstResourcePropertyHelpers
    {
        /// <summary>
        /// Returns all IConstResourceProperty instances of a specific type defined on this resource.
        /// </summary>
        public static IEnumerable<T> GetConstProperties<T>(this IResource res) where T : IConstResourceProperty
        {
            if(res == null)
                throw new ArgumentNullException("res");
            return res.GetConstProperties().OfType<T>();
        }
 
        /// <summary>
        /// Returns all IConstResourceProperty instances on a resource.
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        public static IEnumerable<IConstResourceProperty> GetConstProperties(this IResource res) =>
            GetConstPropertiesImpl(res).ToArray();
        
        static IEnumerable<IConstResourceProperty> 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<IConstResourceProperty>)))
                {
                    var points = (IEnumerable<IConstResourceProperty>)prop.GetValue(res);
                    if (points == null) continue;
                    foreach (var pt in points)
                    {
                        if (pt == null) continue;
 
                        yield return pt;
                    }
                }
            }
        }
    }
}