ClassModel.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System;
 6  using System.Collections.Generic;
 7  using WindowsPackageManager.Interop;
 8  
 9  namespace Microsoft.CmdPal.Ext.WinGet.WindowsPackageManager.Interop;
10  
11  #nullable disable
12  internal sealed class ClassModel
13  {
14      /// <summary>
15      /// Gets the interface for the projected class type generated by CsWinRT
16      /// </summary>
17      public Type InterfaceType { get; init; }
18  
19      /// <summary>
20      /// Gets the projected class type generated by CsWinRT
21      /// </summary>
22      public Type ProjectedClassType { get; init; }
23  
24      /// <summary>
25      /// Gets the Clsids for each context (e.g. OutOfProcProd, OutOfProcDev)
26      /// </summary>
27      public IReadOnlyDictionary<ClsidContext, Guid> Clsids { get; init; }
28  
29      /// <summary>
30      /// Get CLSID based on the provided context
31      /// </summary>
32      /// <param name="context">Context</param>
33      /// <returns>CLSID for the provided context.</returns>
34      /// <exception cref="InvalidOperationException">Throw an exception if the clsid context is not available for the current instance.</exception>
35      public Guid GetClsid(ClsidContext context)
36      {
37          return !Clsids.TryGetValue(context, out var clsid)
38              ? throw new InvalidOperationException($"{ProjectedClassType.FullName} is not implemented in context {context}")
39              : clsid;
40      }
41  
42      /// <summary>
43      /// Get IID corresponding to the COM object
44      /// </summary>
45      /// <returns>IID.</returns>
46      public Guid GetIid() => InterfaceType.GUID;
47  }