summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs
blob: 12b13ac79b8da8623e5d716b67df1fe9199b54d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//

using System;
using System.Diagnostics.Contracts;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices.WindowsRuntime
{
    [ComImport]
    [Guid("60D27C8D-5F61-4CCE-B751-690FAE66AA53")]
    [WindowsRuntimeImport]
    internal interface IManagedActivationFactory
    {
        void RunClassConstructor();
    }

    // A ManangedActivationFactory provides the IActivationFactory implementation for managed types which are
    // constructable via Windows Runtime. Implementation of specialized factory and static WinRT interfaces is
    // provided using VM functionality (see Marshal.InitializeWinRTFactoryObject for details).
    //
    // In order to be activatable via the ManagedActivationFactory type, the type must be decorated with either
    // ActivatableAttribute, or StaticAttribute.
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    internal sealed class ManagedActivationFactory : IActivationFactory, IManagedActivationFactory
    {
        private Type m_type;

        internal ManagedActivationFactory(Type type)
        {
            if (type == null)
                throw new ArgumentNullException(nameof(type));

            // Check whether the type is "exported to WinRT", i.e. it is declared in a managed .winmd and is decorated
            // with at least one ActivatableAttribute or StaticAttribute.
            if (!(type is RuntimeType) || !type.IsExportedToWindowsRuntime)
                throw new ArgumentException(SR.Format(SR.Argument_TypeNotActivatableViaWindowsRuntime, type), nameof(type));

            m_type = type;
        }

        // Activate an instance of the managed type by using its default constructor.
        public object ActivateInstance()
        {
            try
            {
                return Activator.CreateInstance(m_type);
            }
            catch (MissingMethodException)
            {
                // If the type doesn't expose a default constructor, then we fail with E_NOTIMPL
                throw new NotImplementedException();
            }
            catch (TargetInvocationException e)
            {
                throw e.InnerException;
            }
        }

        // Runs the class constructor
        // Currently only Jupiter use this to run class constructor in order to 
        // initialize DependencyProperty objects and do necessary work
        void IManagedActivationFactory.RunClassConstructor()
        {
            RuntimeHelpers.RunClassConstructor(m_type.TypeHandle);
        }
    }
}