Nullable: System.Runtime.InteropServices.CustomMarshalers/WindowsRuntime (#23930)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / src / System / Runtime / InteropServices / WindowsRuntime / ManagedActivationFactory.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #nullable enable
6 using System.Reflection;
7 using System.Runtime.CompilerServices;
8
9 namespace System.Runtime.InteropServices.WindowsRuntime
10 {
11     [ComImport]
12     [Guid("60D27C8D-5F61-4CCE-B751-690FAE66AA53")]
13     [WindowsRuntimeImport]
14     internal interface IManagedActivationFactory
15     {
16         void RunClassConstructor();
17     }
18
19     // A ManangedActivationFactory provides the IActivationFactory implementation for managed types which are
20     // constructable via Windows Runtime. Implementation of specialized factory and static WinRT interfaces is
21     // provided using VM functionality (see Marshal.InitializeWinRTFactoryObject for details).
22     //
23     // In order to be activatable via the ManagedActivationFactory type, the type must be decorated with either
24     // ActivatableAttribute, or StaticAttribute.
25     [ComVisible(true)]
26     [ClassInterface(ClassInterfaceType.None)]
27     internal sealed class ManagedActivationFactory : IActivationFactory, IManagedActivationFactory
28     {
29         private Type m_type;
30
31         internal ManagedActivationFactory(Type type)
32         {
33             if (type == null)
34                 throw new ArgumentNullException(nameof(type));
35
36             // Check whether the type is "exported to WinRT", i.e. it is declared in a managed .winmd and is decorated
37             // with at least one ActivatableAttribute or StaticAttribute.
38             if (!(type is RuntimeType) || !type.IsExportedToWindowsRuntime)
39                 throw new ArgumentException(SR.Format(SR.Argument_TypeNotActivatableViaWindowsRuntime, type), nameof(type));
40
41             m_type = type;
42         }
43
44         // Activate an instance of the managed type by using its default constructor.
45         public object ActivateInstance()
46         {
47             try
48             {
49                 return Activator.CreateInstance(m_type)!;
50             }
51             catch (MissingMethodException)
52             {
53                 // If the type doesn't expose a default constructor, then we fail with E_NOTIMPL
54                 throw new NotImplementedException();
55             }
56             catch (TargetInvocationException e)
57             {
58                 throw e.InnerException!;
59             }
60         }
61
62         // Runs the class constructor
63         // Currently only Jupiter use this to run class constructor in order to 
64         // initialize DependencyProperty objects and do necessary work
65         void IManagedActivationFactory.RunClassConstructor()
66         {
67             RuntimeHelpers.RunClassConstructor(m_type.TypeHandle);
68         }
69     }
70 }