/// <summary>
        /// Initializes a new instance of the <see cref="MethodSemanticsRow"/> struct.
        /// </summary>
        /// <param name="semantics">The semantics.</param>
        /// <param name="method">The method.</param>
        /// <param name="association">The association.</param>
        public MethodSemanticsRow(MethodSemanticsAttributes semantics, Token method,
									Token association)
        {
            Semantics = semantics;
            Method = method;
            Association = association;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodSemanticsRow"/> struct.
        /// </summary>
        /// <param name="semantics">The semantics.</param>
        /// <param name="methodTableIdx">The method table idx.</param>
        /// <param name="associationTableIdx">The association table idx.</param>
        public MethodSemanticsRow(MethodSemanticsAttributes semantics, TokenTypes methodTableIdx,
									TokenTypes associationTableIdx)
        {
            _semantics = semantics;
            _methodTableIdx = methodTableIdx;
            _associationTableIdx = associationTableIdx;
        }
 private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
 {
     if (mdBuilder == null)
     {
         throw new ArgumentNullException("mdBuilder");
     }
     this.m_type.ThrowIfCreated();
     TypeBuilder.DefineMethodSemantics(this.m_module.GetNativeHandle(), this.m_evToken.Token, semantics, mdBuilder.GetToken().Token);
 }
		private void LoadData(CLIFile pFile)
		{
			Semantics = (MethodSemanticsAttributes)pFile.ReadUInt16();
			int methodDefIndex = 0;
			if (pFile.MethodDefTable.Length >= 0xFFFF) methodDefIndex = pFile.ReadInt32() - 1;
			else methodDefIndex = pFile.ReadUInt16() - 1;
			if (methodDefIndex >= 0) Method = pFile.MethodDefTable[methodDefIndex];
			Association.LoadData(pFile);
		}
Example #5
0
        [System.Security.SecurityCritical]  // auto-generated
        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException("mdBuilder");
            }

            m_containingType.ThrowIfCreated();
            TypeBuilder.DefineMethodSemantics(
                m_moduleBuilder.GetNativeHandle(),
                m_prToken.Token,
                semantics,
                mdBuilder.GetToken().Token);
        }
Example #6
0
		public MethodDefOptions(MethodDef method) {
			ImplAttributes = method.ImplAttributes;
			Attributes = method.Attributes;
			SemanticsAttributes = method.SemanticsAttributes;
			RVA = method.RVA;
			Name = method.Name;
			MethodSig = method.MethodSig;
			ImplMap = method.ImplMap;
			CustomAttributes.AddRange(method.CustomAttributes);
			DeclSecurities.AddRange(method.DeclSecurities);
			ParamDefs.AddRange(method.ParamDefs);
			GenericParameters.AddRange(method.GenericParameters);
			Overrides.AddRange(method.Overrides);
		}
Example #7
0
 private bool GetAccessor(MethodSemanticsAttributes methodSemanticsAttribute, out MethodHandle methodHandle)
 {
     foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics)
     {
         MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader);
         if (methodSemantics.Attributes == methodSemanticsAttribute)
         {
             methodHandle = methodSemantics.Method;
             return(true);
         }
     }
     methodHandle = default(MethodHandle);
     return(false);
 }
Example #8
0
        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);
        }
Example #9
0
 public MethodDefOptions(MethodDef method)
 {
     this.ImplAttributes      = method.ImplAttributes;
     this.Attributes          = method.Attributes;
     this.SemanticsAttributes = method.SemanticsAttributes;
     this.RVA       = method.RVA;
     this.Name      = method.Name;
     this.MethodSig = method.MethodSig;
     this.ImplMap   = method.ImplMap;
     this.CustomAttributes.AddRange(method.CustomAttributes);
     this.DeclSecurities.AddRange(method.DeclSecurities);
     this.ParamDefs.AddRange(method.ParamDefs);
     this.GenericParameters.AddRange(method.GenericParameters);
     this.Overrides.AddRange(method.Overrides);
 }
Example #10
0
 public MethodDefOptions(MethodDef method)
 {
     ImplAttributes      = method.ImplAttributes;
     Attributes          = method.Attributes;
     SemanticsAttributes = method.SemanticsAttributes;
     RVA       = method.RVA;
     Name      = method.Name;
     MethodSig = method.MethodSig;
     ImplMap   = method.ImplMap;
     CustomAttributes.AddRange(method.CustomAttributes);
     DeclSecurities.AddRange(method.DeclSecurities);
     ParamDefs.AddRange(method.ParamDefs);
     GenericParameters.AddRange(method.GenericParameters);
     Overrides.AddRange(method.Overrides);
 }
Example #11
0
        [System.Security.SecurityCritical]  // auto-generated
        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);
        }
Example #12
0
        protected override sealed ICliMetadataMethodSemanticsTableRow ReadElementAt(uint index)
        {
            bool gotLock = false;

            Monitor.Enter(this.syncObject, ref gotLock);
            if (index == 0)
            {
                return(null);
            }
            this.fStream.Seek((this.streamOffset + ((index - 1) * this.__size)), SeekOrigin.Begin);
            MethodSemanticsAttributes semantics = ((MethodSemanticsAttributes)(this.reader.ReadUInt16()));
            uint methodIndex;

            switch (this.state)
            {
            case 2:
            case 3:
                methodIndex = this.reader.ReadUInt32();
                break;

            default:
                methodIndex = this.reader.ReadUInt16();
                break;
            }
            uint associationIndex;
            CliMetadataHasSemanticsTag associationSource;

            switch (this.state)
            {
            case 1:
            case 3:
                associationIndex = this.reader.ReadUInt32();
                break;

            default:
                associationIndex = this.reader.ReadUInt16();
                break;
            }
            associationSource = ((CliMetadataHasSemanticsTag)((associationIndex & 1)));
            associationIndex  = (associationIndex >> 1);
            if (gotLock)
            {
                Monitor.Exit(this.syncObject);
            }
            return(new CliMetadataMethodSemanticsLockedTableRow(index, this.state, this.metadataRoot,
                                                                semantics, methodIndex, associationSource,
                                                                associationIndex));
        }
Example #13
0
        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException(nameof(mdBuilder));
            }

            m_type.ThrowIfCreated();
            ModuleBuilder module = m_module;

            TypeBuilder.DefineMethodSemantics(
                new QCallModule(ref module),
                m_evToken.Token,
                semantics,
                mdBuilder.GetToken().Token);
        }
Example #14
0
        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException(nameof(mdBuilder));
            }

            m_containingType.ThrowIfCreated();
            ModuleBuilder module = m_moduleBuilder;

            TypeBuilder.DefineMethodSemantics(
                JitHelpers.GetQCallModuleOnStack(ref module),
                m_prToken.Token,
                semantics,
                mdBuilder.GetToken().Token);
        }
Example #15
0
        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException(nameof(mdBuilder));
            }

            m_containingType.ThrowIfCreated();
            ModuleBuilder module = m_moduleBuilder;

            TypeBuilder.DefineMethodSemantics(
                new QCallModule(ref module),
                m_tkProperty,
                semantics,
                mdBuilder.MetadataToken);
        }
Example #16
0
        private static string DoGetName(MethodDefinition method)
        {
            MethodSemanticsAttributes attrs = method.SemanticsAttributes;

            if (method.Name.Length > 4)                         // this shouldn't be neccesary but Cecil can return ? for method names (eg for Reflector)
            {
                if ((attrs & MethodSemanticsAttributes.Setter) == MethodSemanticsAttributes.Setter)
                {
                    return(method.Name.Substring(4));
                }

                else if ((attrs & MethodSemanticsAttributes.Getter) == MethodSemanticsAttributes.Getter)
                {
                    return(method.Name.Substring(4));
                }
            }

            return(method.Name);
        }
Example #17
0
 // Add method semantics to this event.
 private void AddSemantics(MethodSemanticsAttributes attr,
                           MethodBuilder mdBuilder)
 {
     try
     {
         type.StartSync();
         if (mdBuilder == null)
         {
             throw new ArgumentNullException("mdBuilder");
         }
         lock (typeof(AssemblyBuilder))
         {
             ClrEventAddSemantics
                 (privateData, attr,
                 type.module.GetMethodToken(mdBuilder));
         }
     }
     finally
     {
         type.EndSync();
     }
 }
	// Add method semantics to this event.
	private void AddSemantics(MethodSemanticsAttributes attr,
							  MethodBuilder mdBuilder)
			{
				try
				{
					type.StartSync();
					if(mdBuilder == null)
					{
						throw new ArgumentNullException("mdBuilder");
					}
					lock(typeof(AssemblyBuilder))
					{
						ClrEventAddSemantics
							(privateData, attr,
						 	type.module.GetMethodToken(mdBuilder));
					}
				}
				finally
				{
					type.EndSync();
				}
			}
Example #19
0
        private IMethod ResolveMethod(MethodSemanticsAttributes sem)
        {
            var rows = Loader.Metadata
                       .LookupRows(TableId.MethodSemantics, Schema.MethodSemantics.Association, this.RowIndex(), true)
                       .ToList();

            if (rows.Count == 0)
            {
                return(null);
            }

            var row = rows.FirstOrDefault(
                x => (MethodSemanticsAttributes)x[Schema.MethodSemantics.Semantics].Value == sem);

            if (row == null)
            {
                return(null);
            }

            var methodIndex = row[Schema.MethodSemantics.Method].Index - 1;

            return(Loader.Methods[methodIndex]);
        }
        public MethodSemanticsLookup(MetadataReader metadata, MethodSemanticsAttributes filter = CsharpAccessors)
        {
            if ((filter & MethodSemanticsAttributes.Other) != 0)
            {
                throw new NotSupportedException("SRM doesn't provide access to 'other' accessors");
            }

            _entries = new List <Entry>(metadata.GetTableRowCount(TableIndex.MethodSemantics));
            foreach (var propHandle in metadata.PropertyDefinitions)
            {
                var prop      = metadata.GetPropertyDefinition(propHandle);
                var accessors = prop.GetAccessors();
                AddEntry(MethodSemanticsAttributes.Getter, accessors.Getter, propHandle);
                AddEntry(MethodSemanticsAttributes.Setter, accessors.Setter, propHandle);
            }

            foreach (var eventHandle in metadata.EventDefinitions)
            {
                var ev        = metadata.GetEventDefinition(eventHandle);
                var accessors = ev.GetAccessors();
                AddEntry(MethodSemanticsAttributes.Adder, accessors.Adder, eventHandle);
                AddEntry(MethodSemanticsAttributes.Remover, accessors.Remover, eventHandle);
                AddEntry(MethodSemanticsAttributes.Raiser, accessors.Raiser, eventHandle);
            }

            _entries.Sort();

            void AddEntry(MethodSemanticsAttributes semantics, MethodDefinitionHandle method, EntityHandle association)
            {
                if ((semantics & filter) == 0 || method.IsNil)
                {
                    return;
                }

                _entries.Add(new Entry(semantics, method, association));
            }
        }
        } // Write

        public static void Write(this NativeWriter writer, MethodSemanticsAttributes value)
        {
            writer.WriteUnsigned((uint)value);
        } // Write
Example #22
0
 /// <summary>
 /// Creates a new row for the method semantics table.
 /// </summary>
 /// <param name="attributes">The semantic attributes that are assigned to the method.</param>
 /// <param name="method">The index into the method definition table indicating the method that was assigned
 /// special semantics.</param>
 /// <param name="association">The index into the method definition table indicating the method that was assigned
 /// special semantics.</param>
 public MethodSemanticsRow(MethodSemanticsAttributes attributes, uint method, uint association)
 {
     Attributes  = attributes;
     Method      = method;
     Association = association;
 }
Example #23
0
 public Entry(MethodSemanticsAttributes semantics, MethodDefinitionHandle method, EntityHandle association)
 {
     Semantics       = semantics;
     MethodRowNumber = MetadataTokens.GetRowNumber(method);
     Association     = association;
 }
Example #24
0
 public MethodSemantics(MethodDefinition method, MetaDataMember association, MethodSemanticsAttributes attributes)
     : base(new MetaDataRow((uint)attributes, method.TableIndex, (uint)0))
 {
     this._method = method;
     this._association = association;
 }
Example #25
0
 extern public static bool HasSemantics
     (IntPtr item, MethodSemanticsAttributes type, bool nonPublic);
Example #26
0
 public static bool GetSemantics(this MethodDefinition self, MethodSemanticsAttributes semantics) =>
 (((ushort)(self.SemanticsAttributes & semantics)) != 0);
	extern private static void ClrPropertyAddSemantics
			(IntPtr item, MethodSemanticsAttributes attr,
			 MethodToken token);
Example #28
0
 public static bool GetSemantics(this MethodDefinition self, MethodSemanticsAttributes semantics)
 {
     return((self.SemanticsAttributes & semantics) != 0);
 }
Example #29
0
 private void Add(MethodSemanticsAttributes a)
 {
 }
	extern public static MethodInfo GetSemantics
			(IntPtr item, MethodSemanticsAttributes type, bool nonPublic);
Example #31
0
		/// <summary>
		///  Fills the row from the array of bytes.
		/// </summary>
		unsafe public void FromRawData(byte [] buff, int offs)
		{
			if (buff == null) throw new Exception("buff == null");
			if (offs + Size > buff.Length) throw new Exception("bounds");

		
			this.Semantics = (MethodSemanticsAttributes) LEBitConverter.ToUInt16(buff, offs);
			offs += sizeof (ushort);
			this.Method = LEBitConverter.ToInt32(buff, offs);
			offs += 4;
			this.Association = TabsDecoder.DecodeToken(CodedTokenId.HasSemantics, LEBitConverter.ToInt32(buff, offs));
			
		}
Example #32
0
		public static bool GetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics)
		{
			return (self.SemanticsAttributes & semantics) != 0;
		}
 public MethodSemanticsRow CreateMethodSemanticsRow(MethodSemanticsAttributes _semantics, uint _method, MetadataToken _association)
 {
     MethodSemanticsRow row = new MethodSemanticsRow ();
     row.Semantics = _semantics;
     row.Method = _method;
     row.Association = _association;
     return row;
 }
	extern public static bool HasSemantics
			(IntPtr item, MethodSemanticsAttributes type, bool nonPublic);
Example #35
0
    private MethodDefinition ExposeMethod(TypeDefinition typeToProcess, FieldReference fieldToProcess, MethodReference methodToExpose, ExposeMode exposeMode, MethodSemanticsAttributes semanticAttributes)
    {
        var typeToExpose = fieldToProcess.FieldType;

        var             genericTypeToExpose = typeToExpose as GenericInstanceType;
        MethodReference methodToExposeWithResolvedGenerics = null;

        if (genericTypeToExpose != null)
        {
            methodToExposeWithResolvedGenerics = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: true);
            methodToExpose = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: false);
        }
        var name = exposeMode == ExposeMode.ImplementExplicit ? typeToExpose.FullName + "." + methodToExpose.Name : methodToExpose.Name;

        MethodAttributes methodAttributes = (exposeMode == ExposeMode.ImplementExplicit ? MethodAttributes.Private : MethodAttributes.Public);

        if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
        {
            methodAttributes |= MethodAttributes.Final;
        }
        methodAttributes |= MethodAttributes.HideBySig;
        if (semanticAttributes != MethodSemanticsAttributes.None)
        {
            methodAttributes |= MethodAttributes.SpecialName;
        }
        if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
        {
            methodAttributes |= MethodAttributes.NewSlot | MethodAttributes.Virtual;
        }

        var method = new MethodDefinition(name, methodAttributes, (methodToExposeWithResolvedGenerics ?? methodToExpose).ReturnType);

        foreach (var genericParameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).GenericParameters)
        {
            method.GenericParameters.Add(new GenericParameter(genericParameter.Name, method));
        }
        foreach (var parameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).Parameters)
        {
            method.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, parameter.ParameterType));
        }

        var existingMethod = typeToProcess.GetMethodLike(method);

        if (existingMethod != null)
        {
            return(existingMethod);
        }

        bool methodReturnsVoid = method.ReturnType.FullName == ModuleDefinition.TypeSystem.Void.FullName;

        if (!methodReturnsVoid)
        {
            method.Body.Variables.Add(new VariableDefinition(method.ReturnType));
            method.Body.InitLocals = true;
        }

        var instructions = method.Body.Instructions;

        instructions.Add(Instruction.Create(OpCodes.Nop));
        instructions.Add(Instruction.Create(OpCodes.Ldarg_0));         // Load this
        instructions.Add(Instruction.Create(OpCodes.Ldfld,
                                            fieldToProcess.DeclaringType.HasGenericParameters ?
                                            fieldToProcess.With(declaringType: fieldToProcess.DeclaringType.MakeGenericInstanceTypeWithGenericParametersAsGenericArguments())
                        : fieldToProcess)
                         );

        if (methodToExpose.Parameters.Count >= 1)
        {
            instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
        }
        if (methodToExpose.Parameters.Count >= 2)
        {
            instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
        }
        if (methodToExpose.Parameters.Count >= 3)
        {
            instructions.Add(Instruction.Create(OpCodes.Ldarg_3));
        }
        if (methodToExpose.Parameters.Count >= 4)
        {
            for (var i = 3; i < methodToExpose.Parameters.Count; i++)
            {
                instructions.Add(Instruction.Create(OpCodes.Ldarg_S, i + 1));
            }
        }
        instructions.Add(Instruction.Create(OpCodes.Callvirt, methodToExpose.HasGenericParameters ? methodToExpose.MakeGenericInstanceMethod(method.GenericParameters.ToArray()) : methodToExpose));
        if (methodReturnsVoid)
        {
            instructions.Add(Instruction.Create(OpCodes.Nop));
        }
        else
        {
            instructions.Add(Instruction.Create(OpCodes.Stloc_0));
            var inst = Instruction.Create(OpCodes.Ldloc_0);
            instructions.Add(Instruction.Create(OpCodes.Br_S, inst));
            instructions.Add(inst);
        }
        instructions.Add(Instruction.Create(OpCodes.Ret));

        if (exposeMode == ExposeMode.ImplementExplicit)
        {
            method.Overrides.Add(methodToExpose);
        }

        AddFodyGeneratedAttributes(method);

        typeToProcess.Methods.Add(method);
        return(method);
    }
Example #36
0
 extern private static void ClrPropertyAddSemantics
     (IntPtr item, MethodSemanticsAttributes attr,
     MethodToken token);
Example #37
0
	private MethodDefinition ExposeMethod(TypeDefinition typeToProcess, FieldReference fieldToProcess, MethodReference methodToExpose, ExposeMode exposeMode, MethodSemanticsAttributes semanticAttributes)
	{
		var typeToExpose = fieldToProcess.FieldType;

		var genericTypeToExpose = typeToExpose as GenericInstanceType;
		MethodReference methodToExposeWithResolvedGenerics = null;

		if (genericTypeToExpose != null)
		{
			methodToExposeWithResolvedGenerics = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: true);
			methodToExpose = methodToExpose.With(declaringType: methodToExpose.DeclaringType.MakeGenericInstanceType(genericTypeToExpose.GenericArguments.ToArray()), resolveGenericReturnTypeAndParameterTypes: false);
		}
		var name = exposeMode == ExposeMode.ImplementExplicit ? typeToExpose.FullName + "." + methodToExpose.Name : methodToExpose.Name;

		MethodAttributes methodAttributes = (exposeMode == ExposeMode.ImplementExplicit ? MethodAttributes.Private : MethodAttributes.Public);
		if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
			methodAttributes |= MethodAttributes.Final;
		methodAttributes |= MethodAttributes.HideBySig;
		if (semanticAttributes != MethodSemanticsAttributes.None)
			methodAttributes |= MethodAttributes.SpecialName;
		if (exposeMode == ExposeMode.ImplementImplicit || exposeMode == ExposeMode.ImplementExplicit)
			methodAttributes |= MethodAttributes.NewSlot | MethodAttributes.Virtual;

		var method = new MethodDefinition(name, methodAttributes, (methodToExposeWithResolvedGenerics ?? methodToExpose).ReturnType);
		foreach (var genericParameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).GenericParameters)
		{
			method.GenericParameters.Add(new GenericParameter(genericParameter.Name, method));
		}
		foreach (var parameter in (methodToExposeWithResolvedGenerics ?? methodToExpose).Parameters)
		{
			method.Parameters.Add(new ParameterDefinition(parameter.Name, parameter.Attributes, parameter.ParameterType));
		}

		var existingMethod = typeToProcess.GetMethodLike(method);
		if (existingMethod != null)
			return existingMethod;

		bool methodReturnsVoid = method.ReturnType.FullName == ModuleDefinition.TypeSystem.Void.FullName;

		if (!methodReturnsVoid)
		{
			method.Body.Variables.Add(new VariableDefinition(method.ReturnType));
			method.Body.InitLocals = true;
		}

		var instructions = method.Body.Instructions;
		instructions.Add(Instruction.Create(OpCodes.Nop));
		instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); // Load this
		instructions.Add(Instruction.Create(OpCodes.Ldfld,
			fieldToProcess.DeclaringType.HasGenericParameters ?
			fieldToProcess.With(declaringType: fieldToProcess.DeclaringType.MakeGenericInstanceTypeWithGenericParametersAsGenericArguments())
			: fieldToProcess)
		);

		if (methodToExpose.Parameters.Count >= 1) instructions.Add(Instruction.Create(OpCodes.Ldarg_1));
		if (methodToExpose.Parameters.Count >= 2) instructions.Add(Instruction.Create(OpCodes.Ldarg_2));
		if (methodToExpose.Parameters.Count >= 3) instructions.Add(Instruction.Create(OpCodes.Ldarg_3));
		if (methodToExpose.Parameters.Count >= 4)
		{
			for (var i = 3; i < methodToExpose.Parameters.Count; i++)
			{
				instructions.Add(Instruction.Create(OpCodes.Ldarg_S, i + 1));
			}
		}
		instructions.Add(Instruction.Create(OpCodes.Callvirt, methodToExpose.HasGenericParameters ? methodToExpose.MakeGenericInstanceMethod(method.GenericParameters.ToArray()) : methodToExpose));
		if (methodReturnsVoid)
			instructions.Add(Instruction.Create(OpCodes.Nop));
		else
		{
			instructions.Add(Instruction.Create(OpCodes.Stloc_0));
			var inst = Instruction.Create(OpCodes.Ldloc_0);
			instructions.Add(Instruction.Create(OpCodes.Br_S, inst));
			instructions.Add(inst);
		}
		instructions.Add(Instruction.Create(OpCodes.Ret));

		if (exposeMode == ExposeMode.ImplementExplicit)
			method.Overrides.Add(methodToExpose);

		AddFodyGeneratedAttributes(method);

		typeToProcess.Methods.Add(method);
		return method;
	}
Example #38
0
 /// <summary>
 /// Creates a new method semantics object.
 /// </summary>
 /// <param name="method">The method to give special semantics.</param>
 /// <param name="attributes">The type of semantics to assign.</param>
 public MethodSemantics(MethodDefinition method, MethodSemanticsAttributes attributes)
     : this(new MetadataToken(TableIndex.MethodSemantics, 0))
 {
     Method     = method ?? throw new ArgumentNullException(nameof(method));
     Attributes = attributes;
 }
        void WriteSemantic(MethodSemanticsAttributes attrs,
            IMetadataTokenProvider member, MethodDefinition meth)
        {
            MethodSemanticsTable msTable = m_tableWriter.GetMethodSemanticsTable ();
            MethodSemanticsRow msRow = m_rowWriter.CreateMethodSemanticsRow (
                attrs,
                GetRidFor (meth),
                member.MetadataToken);

            msTable.Rows.Add (msRow);
        }
Example #40
0
 internal CliMetadataMethodSemanticsLockedTableRow(uint index, byte state, ICliMetadataRoot metadataRoot, MethodSemanticsAttributes semantics, uint methodIndex, CliMetadataHasSemanticsTag associationSource, uint associationIndex)
 {
     this.index             = index;
     this.metadataRoot      = metadataRoot;
     this.state             = state;
     this.semantics         = semantics;
     this.methodIndex       = methodIndex;
     this.associationSource = associationSource;
     this.associationIndex  = associationIndex;
 }
Example #41
0
 private bool GetAccessor(MethodSemanticsAttributes methodSemanticsAttribute, out MethodHandle methodHandle)
 {
     foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics)
     {
         MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader);
         if (methodSemantics.Attributes == methodSemanticsAttribute)
         {
             methodHandle = methodSemantics.Method;
             return true;
         }
     }
     methodHandle = default(MethodHandle);
     return false;
 }
        internal static void AssignAssociates(MetadataImport scope, int mdPropEvent, RuntimeType declaringType, RuntimeType reflectedType, out RuntimeMethodInfo addOn, out RuntimeMethodInfo removeOn, out RuntimeMethodInfo fireOn, out RuntimeMethodInfo getter, out RuntimeMethodInfo setter, out MethodInfo[] other, out bool composedOfAllPrivateMethods, out BindingFlags bindingFlags)
        {
            RuntimeMethodInfo runtimeMethodInfo;

            setter   = (runtimeMethodInfo = null);
            getter   = (runtimeMethodInfo = runtimeMethodInfo);
            fireOn   = (runtimeMethodInfo = runtimeMethodInfo);
            removeOn = (runtimeMethodInfo = runtimeMethodInfo);
            addOn    = runtimeMethodInfo;
            Associates.Attributes attributes = Associates.Attributes.ComposedOfAllVirtualMethods | Associates.Attributes.ComposedOfAllPrivateMethods | Associates.Attributes.ComposedOfNoPublicMembers | Associates.Attributes.ComposedOfNoStaticMembers;
            while (RuntimeTypeHandle.IsGenericVariable(reflectedType))
            {
                reflectedType = (RuntimeType)reflectedType.BaseType;
            }
            bool isInherited        = declaringType != reflectedType;
            List <MethodInfo>  list = null;
            MetadataEnumResult metadataEnumResult;

            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out metadataEnumResult);
            int num = metadataEnumResult.Length / 2;

            for (int i = 0; i < num; i++)
            {
                int tkMethod = metadataEnumResult[i * 2];
                MethodSemanticsAttributes methodSemanticsAttributes = (MethodSemanticsAttributes)metadataEnumResult[i * 2 + 1];
                RuntimeMethodInfo         runtimeMethodInfo2        = Associates.AssignAssociates(tkMethod, declaringType, reflectedType);
                if (!(runtimeMethodInfo2 == null))
                {
                    MethodAttributes attributes2      = runtimeMethodInfo2.Attributes;
                    bool             flag             = (attributes2 & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                    bool             flag2            = (attributes2 & MethodAttributes.Virtual) > MethodAttributes.PrivateScope;
                    MethodAttributes methodAttributes = attributes2 & MethodAttributes.MemberAccessMask;
                    bool             flag3            = methodAttributes == MethodAttributes.Public;
                    bool             flag4            = (attributes2 & MethodAttributes.Static) > MethodAttributes.PrivateScope;
                    if (flag3)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfNoPublicMembers;
                        attributes &= ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    else if (!flag)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfAllPrivateMethods;
                    }
                    if (flag4)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfNoStaticMembers;
                    }
                    if (!flag2)
                    {
                        attributes &= ~Associates.Attributes.ComposedOfAllVirtualMethods;
                    }
                    if (methodSemanticsAttributes == MethodSemanticsAttributes.Setter)
                    {
                        setter = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.Getter)
                    {
                        getter = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.Fire)
                    {
                        fireOn = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.AddOn)
                    {
                        addOn = runtimeMethodInfo2;
                    }
                    else if (methodSemanticsAttributes == MethodSemanticsAttributes.RemoveOn)
                    {
                        removeOn = runtimeMethodInfo2;
                    }
                    else
                    {
                        if (list == null)
                        {
                            list = new List <MethodInfo>(num);
                        }
                        list.Add(runtimeMethodInfo2);
                    }
                }
            }
            bool isPublic = (attributes & Associates.Attributes.ComposedOfNoPublicMembers) == (Associates.Attributes) 0;
            bool isStatic = (attributes & Associates.Attributes.ComposedOfNoStaticMembers) == (Associates.Attributes) 0;

            bindingFlags = RuntimeType.FilterPreCalculate(isPublic, isInherited, isStatic);
            composedOfAllPrivateMethods = ((attributes & Associates.Attributes.ComposedOfAllPrivateMethods) > (Associates.Attributes) 0);
            other = ((list != null) ? list.ToArray() : null);
        }
        } // Read

        public static uint Read(this NativeReader reader, uint offset, out MethodSemanticsAttributes value)
        {
            uint ivalue;
            offset = reader.DecodeUnsigned(offset, out ivalue);
            value = (MethodSemanticsAttributes)ivalue;
            return offset;
        } // Read
Example #44
0
 extern public static MethodInfo GetSemantics
     (IntPtr item, MethodSemanticsAttributes type, bool nonPublic);
Example #45
0
 public MethodSemanticsRow(MethodSemanticsAttributes attributes, MetadataToken method, MetadataToken association) : this()
 {
     Attributes  = attributes;
     Method      = method;
     Association = association;
 }
Example #46
0
        private bool GetAccessor(MethodSemanticsAttributes methodSemanticsAttribute, out MethodHandle methodHandle)
        {
            bool inherited = !_reflectedType.Equals(_contextTypeInfo);

            foreach (MethodSemanticsHandle methodSemanticsHandle in _property.MethodSemantics)
            {
                MethodSemantics methodSemantics = methodSemanticsHandle.GetMethodSemantics(_reader);
                if (methodSemantics.Attributes == methodSemanticsAttribute)
                {
                    methodHandle = methodSemantics.Method;

                    if (inherited)
                    {
                        MethodAttributes flags = methodHandle.GetMethod(_reader).Flags;
                        if ((flags & MethodAttributes.MemberAccessMask) == MethodAttributes.Private)
                            continue;
                    }

                    return true;
                }
            }
            methodHandle = default(MethodHandle);
            return false;
        }
 public void Read(ClrModuleReader reader)
 {
     this.Semantics   = (MethodSemanticsAttributes)reader.Binary.ReadUInt16();
     this.Method      = reader.ReadTableIndex(TableKind.MethodDef);
     this.Association = reader.ReadCodedIndex <HasSemantics>();
 }
Example #48
0
		/// <summary>
		/// Reads a raw <c>MethodSemantics</c> row
		/// </summary>
		/// <param name="rid">Row ID</param>
		/// <param name="semantic"></param>
		/// <returns></returns>
		internal uint ReadMethodSemanticsRow(uint rid, out MethodSemanticsAttributes semantic) {
			var table = MethodSemanticsTable;
#if THREAD_SAFE
			theLock.EnterWriteLock(); try {
#endif
			var reader = GetReader_NoLock(table, rid);
			var columns = table.TableInfo.Columns;
			semantic = (MethodSemanticsAttributes)reader.ReadUInt16();
			return columns[1].Read(reader);
#if THREAD_SAFE
			} finally { theLock.ExitWriteLock(); }
#endif
		}
Example #49
0
		public static void SetSemantics (this MethodDefinition self, MethodSemanticsAttributes semantics, bool value)
		{
			if (value)
				self.SemanticsAttributes |= semantics;
			else
				self.SemanticsAttributes &= ~semantics;
		}
Example #50
0
 public void SetAccessorOwner(IMember owner, MethodSemanticsAttributes accKind)
 {
     this.AccessorOwner = owner ?? throw new ArgumentNullException(nameof(owner));
     this.AccessorKind  = accKind;
 }
Example #51
0
 internal static extern void DefineMethodSemantics(RuntimeModule module, int tkAssociation, 
     MethodSemanticsAttributes semantics, int tkMethod);
Example #52
0
 public MethodSemantics(MethodDefinition method, MetaDataMember association, MethodSemanticsAttributes attributes)
     : base(new MetaDataRow((uint)attributes, method.TableIndex, (uint) 0))
 {
     this._method      = method;
     this._association = association;
 }
 public void Read(ClrModuleReader reader)
 {
     this.Semantics = (MethodSemanticsAttributes)reader.Binary.ReadUInt16();
     this.Method = reader.ReadTableIndex(TableKind.MethodDef);
     this.Association = reader.ReadCodedIndex<HasSemantics>();
 }
Example #54
0
        [System.Security.SecurityCritical]  // auto-generated
        private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttributes semantics)
        {
            if (mdBuilder == null)
            {
                throw new ArgumentNullException(nameof(mdBuilder));
            }

            m_containingType.ThrowIfCreated();
            TypeBuilder.DefineMethodSemantics(
                m_moduleBuilder.GetNativeHandle(),
                m_prToken.Token,
                semantics,
                mdBuilder.GetToken().Token);
        }    
Example #55
0
        internal static void AssignAssociates(
            MetadataImport scope,
            int mdPropEvent,
            RuntimeType declaringType,
            RuntimeType reflectedType,
            out RuntimeMethodInfo?addOn,
            out RuntimeMethodInfo?removeOn,
            out RuntimeMethodInfo?fireOn,
            out RuntimeMethodInfo?getter,
            out RuntimeMethodInfo?setter,
            out MethodInfo[]?other,
            out bool composedOfAllPrivateMethods,
            out BindingFlags bindingFlags)
        {
            addOn = removeOn = fireOn = getter = setter = null;

            Attributes attributes =
                Attributes.ComposedOfAllPrivateMethods |
                Attributes.ComposedOfAllVirtualMethods |
                Attributes.ComposedOfNoPublicMembers |
                Attributes.ComposedOfNoStaticMembers;

            while (RuntimeTypeHandle.IsGenericVariable(reflectedType))
            {
                reflectedType = (RuntimeType)reflectedType.BaseType !;
            }

            bool isInherited = declaringType != reflectedType;

            List <MethodInfo>?otherList = null;

            scope.Enum(MetadataTokenType.MethodDef, mdPropEvent, out MetadataEnumResult associatesData);

            int cAssociates = associatesData.Length / 2;

            for (int i = 0; i < cAssociates; i++)
            {
                int methodDefToken = associatesData[i * 2];
                MethodSemanticsAttributes semantics = (MethodSemanticsAttributes)associatesData[i * 2 + 1];

                #region Assign each associate
                RuntimeMethodInfo?associateMethod =
                    AssignAssociates(methodDefToken, declaringType, reflectedType);

                if (associateMethod == null)
                {
                    continue;
                }

                MethodAttributes methAttr  = associateMethod.Attributes;
                bool             isPrivate = (methAttr & MethodAttributes.MemberAccessMask) == MethodAttributes.Private;
                bool             isVirtual = (methAttr & MethodAttributes.Virtual) != 0;

                MethodAttributes visibility = methAttr & MethodAttributes.MemberAccessMask;
                bool             isPublic   = visibility == MethodAttributes.Public;
                bool             isStatic   = (methAttr & MethodAttributes.Static) != 0;

                if (isPublic)
                {
                    attributes &= ~Attributes.ComposedOfNoPublicMembers;
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }
                else if (!isPrivate)
                {
                    attributes &= ~Attributes.ComposedOfAllPrivateMethods;
                }

                if (isStatic)
                {
                    attributes &= ~Attributes.ComposedOfNoStaticMembers;
                }

                if (!isVirtual)
                {
                    attributes &= ~Attributes.ComposedOfAllVirtualMethods;
                }
                #endregion

                if (semantics == MethodSemanticsAttributes.Setter)
                {
                    setter = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.Getter)
                {
                    getter = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.Fire)
                {
                    fireOn = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.AddOn)
                {
                    addOn = associateMethod;
                }
                else if (semantics == MethodSemanticsAttributes.RemoveOn)
                {
                    removeOn = associateMethod;
                }
                else
                {
                    otherList ??= new List <MethodInfo>(cAssociates);
                    otherList.Add(associateMethod);
                }
            }

            bool isPseudoPublic = (attributes & Attributes.ComposedOfNoPublicMembers) == 0;
            bool isPseudoStatic = (attributes & Attributes.ComposedOfNoStaticMembers) == 0;
            bindingFlags = RuntimeType.FilterPreCalculate(isPseudoPublic, isInherited, isPseudoStatic);

            composedOfAllPrivateMethods = (attributes & Attributes.ComposedOfAllPrivateMethods) != 0;

            other = otherList?.ToArray();
        }
	extern private static void ClrEventAddSemantics
			(IntPtr eventInfo, MethodSemanticsAttributes attr,
			 MethodToken token);