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.
8 using System.Reflection;
9 using System.Runtime.InteropServices;
10 using System.Security;
11 using System.Runtime.CompilerServices;
13 namespace System.Runtime.InteropServices.WindowsRuntime
16 [Guid("60D27C8D-5F61-4CCE-B751-690FAE66AA53")]
17 [WindowsRuntimeImport]
18 internal interface IManagedActivationFactory
20 void RunClassConstructor();
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).
27 // In order to be activatable via the ManagedActivationFactory type, the type must be decorated with either
28 // ActivatableAttribute, or StaticAttribute.
30 [ClassInterface(ClassInterfaceType.None)]
31 internal sealed class ManagedActivationFactory : IActivationFactory, IManagedActivationFactory
35 internal ManagedActivationFactory(Type type)
38 throw new ArgumentNullException(nameof(type));
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));
48 // Activate an instance of the managed type by using its default constructor.
49 public object ActivateInstance()
53 return Activator.CreateInstance(m_type);
55 catch (MissingMethodException)
57 // If the type doesn't expose a default constructor, then we fail with E_NOTIMPL
58 throw new NotImplementedException();
60 catch (TargetInvocationException e)
62 throw e.InnerException;
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()
71 RuntimeHelpers.RunClassConstructor(m_type.TypeHandle);