< Summary

Information
Class: SolidEdgeCommunity.Runtime.InteropServices.ComObject
Assembly: SolidEdgeCommunity
File(s): D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\Runtime\InteropServices\ComObject.cs
Line coverage
91%
Covered lines: 34
Uncovered lines: 3
Coverable lines: 37
Total lines: 126
Line coverage: 91.8%
Branch coverage
85%
Covered branches: 17
Total branches: 20
Branch coverage: 85%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetITypeInfo(...)75%4475%
GetPropertyValue(...)50%22100%
GetPropertyValue(...)100%2285.71%
GetType(...)91.66%121295.45%

File(s)

D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\Runtime\InteropServices\ComObject.cs

#LineLine coverage
 1using SolidEdgeCommunity.Runtime.InteropServices.ComTypes;
 2using System;
 3using System.Linq;
 4using System.Runtime.InteropServices;
 5using System.Runtime.InteropServices.ComTypes;
 6
 7namespace SolidEdgeCommunity.Runtime.InteropServices;
 8
 9/// <summary>
 10/// COM object wrapper class.
 11/// </summary>
 12public static class ComObject
 13{
 14    private const int LOCALE_SYSTEM_DEFAULT = 2048;
 15
 16    /// <summary>
 17    /// Using IDispatch, returns the ITypeInfo of the specified object.
 18    /// </summary>
 19    /// <param name="comObject"></param>
 20    /// <returns></returns>
 21    public static ITypeInfo GetITypeInfo(object comObject)
 22    {
 323        if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException();
 24
 125        if (comObject is IDispatch dispatch)
 26        {
 127            return dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT);
 28        }
 29
 030        return null;
 31    }
 32
 33    /// <summary>
 34    /// Returns a strongly typed property by name using the specified COM object.
 35    /// </summary>
 36    /// <typeparam name="T">The type of the property to return.</typeparam>
 37    /// <param name="comObject"></param>
 38    /// <param name="name">The name of the property to retrieve.</param>
 39    /// <returns></returns>
 40    public static T GetPropertyValue<T>(object comObject, string name)
 41    {
 142        if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException();
 43
 144        var type = comObject.GetType();
 145        var value = type.InvokeMember(name, System.Reflection.BindingFlags.GetProperty, null, comObject, null);
 46
 147        return (T)value;
 48    }
 49
 50    /// <summary>
 51    /// Returns a strongly typed property by name using the specified COM object.
 52    /// </summary>
 53    /// <typeparam name="T">The type of the property to return.</typeparam>
 54    /// <param name="comObject"></param>
 55    /// <param name="name">The name of the property to retrieve.</param>
 56    /// <param name="defaultValue">The value to return if the property does not exist.</param>
 57    /// <returns></returns>
 58    public static T GetPropertyValue<T>(object comObject, string name, T defaultValue)
 59    {
 360        if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException();
 61
 162        var type = comObject.GetType();
 63
 64        try
 65        {
 166            var value = type.InvokeMember(name, System.Reflection.BindingFlags.GetProperty, null, comObject, null);
 067            return (T)value;
 68        }
 169        catch
 70        {
 171            return defaultValue;
 72        }
 173    }
 74
 75    /// <summary>
 76    /// Using IDispatch, determine the managed type of the specified object.
 77    /// </summary>
 78    /// <param name="comObject"></param>
 79    /// <returns></returns>
 80    public static Type GetType(object comObject)
 81    {
 382        if (!System.Runtime.InteropServices.Marshal.IsComObject(comObject)) throw new InvalidComObjectException();
 83
 184        Type type = null;
 185        ITypeInfo typeInfo = null;
 186        var pTypeAttr = IntPtr.Zero;
 187        var typeAttr = default(TYPEATTR);
 88
 89        try
 90        {
 191            if (comObject is IDispatch dispatch)
 92            {
 193                typeInfo = dispatch.GetTypeInfo(0, LOCALE_SYSTEM_DEFAULT);
 194                typeInfo.GetTypeAttr(out pTypeAttr);
 195                typeAttr = System.Runtime.InteropServices.Marshal.PtrToStructure<TYPEATTR>(pTypeAttr);
 96
 97                // Type can technically be defined in any loaded assembly.
 198                var assemblies = AppDomain.CurrentDomain.GetAssemblies();
 99
 100                // Scan each assembly for a type with a matching GUID.
 146101                foreach (var assembly in assemblies)
 102                {
 72103                    type = assembly.GetTypes()
 16884104                        .Where(x => x.IsInterface)
 2892105                        .FirstOrDefault(x => x.GUID.Equals(typeAttr.guid));
 106
 72107                    if (type != null)
 108                    {
 109                        // Found what we're looking for so break out of the loop.
 0110                        break;
 111                    }
 112                }
 113            }
 1114        }
 115        finally
 116        {
 1117            if (typeInfo != null)
 118            {
 1119                typeInfo.ReleaseTypeAttr(pTypeAttr);
 1120                System.Runtime.InteropServices.Marshal.ReleaseComObject(typeInfo);
 121            }
 1122        }
 123
 1124        return type;
 125    }
 126}