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