summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Reflection/Emit/EventBuilder.cs
blob: 449b20824cd5c61cf4ff7fda26a45fdc424bdd1a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// 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.

/*============================================================
**
** 
** 
**
**
** Eventbuilder is for client to define eevnts for a class
**
** 
===========================================================*/
namespace System.Reflection.Emit {
    
    using System;
    using System.Reflection;
    using System.Security.Permissions;
    using System.Runtime.InteropServices;
    using System.Diagnostics.Contracts;
    
    // 
    // A EventBuilder is always associated with a TypeBuilder.  The TypeBuilder.DefineEvent
    // method will return a new EventBuilder to a client.
    // 
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(_EventBuilder))]
[System.Runtime.InteropServices.ComVisible(true)]
    public sealed class EventBuilder : _EventBuilder
    { 
    
        // Make a private constructor so these cannot be constructed externally.
        private EventBuilder() {}
        
        // Constructs a EventBuilder.  
        //
        internal EventBuilder(
            ModuleBuilder    mod,                    // the module containing this EventBuilder        
            String            name,                    // Event name
            EventAttributes attr,                    // event attribute such as Public, Private, and Protected defined above
            //int            eventType,                // event type
            TypeBuilder     type,                    // containing type
            EventToken        evToken)
        {           
            m_name = name;
            m_module = mod;
            m_attributes = attr;
            m_evToken = evToken;
            m_type = type;
        }
    
        // Return the Token for this event within the TypeBuilder that the
        // event is defined within.
        public EventToken GetEventToken()
        {
            return m_evToken;
        }

        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException(nameof(mdBuilder));
            }
            Contract.EndContractBlock();

            m_type.ThrowIfCreated();
            TypeBuilder.DefineMethodSemantics(
                m_module.GetNativeHandle(),
                m_evToken.Token,
                semantics,
                mdBuilder.GetToken().Token);
        }

        public void SetAddOnMethod(MethodBuilder mdBuilder)
        {
            SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.AddOn);
        }
        
        public void SetRemoveOnMethod(MethodBuilder mdBuilder)
        {
            SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.RemoveOn);
        }
        
        public void SetRaiseMethod(MethodBuilder mdBuilder)
        {
            SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Fire);
        }
       
        public void AddOtherMethod(MethodBuilder mdBuilder)
        {
            SetMethodSemantics(mdBuilder, MethodSemanticsAttributes.Other);
        }
    
        // Use this function if client decides to form the custom attribute blob themselves

[System.Runtime.InteropServices.ComVisible(true)]
        public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
        {
            if (con == null)
                throw new ArgumentNullException(nameof(con));
            if (binaryAttribute == null)
                throw new ArgumentNullException(nameof(binaryAttribute));
            Contract.EndContractBlock();
            m_type.ThrowIfCreated();

            TypeBuilder.DefineCustomAttribute(
                m_module,
                m_evToken.Token,
                m_module.GetConstructorToken(con).Token,
                binaryAttribute,
                false, false);
        }

        // Use this function if client wishes to build CustomAttribute using CustomAttributeBuilder
        public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
        {
            if (customBuilder == null)
            {
                throw new ArgumentNullException(nameof(customBuilder));
            }
            Contract.EndContractBlock();
            m_type.ThrowIfCreated();
            customBuilder.CreateCustomAttribute(m_module, m_evToken.Token);
        }

        // These are package private so that TypeBuilder can access them.
        private String              m_name;         // The name of the event
        private EventToken          m_evToken;      // The token of this event
        private ModuleBuilder       m_module;
        private EventAttributes     m_attributes;
        private TypeBuilder         m_type;       
    }
}