Method.cs :  » Aspect-Oriented-Frameworks » Runtime-Assembly-Instrumentation-Library » rail » Reflect » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Aspect Oriented Frameworks » Runtime Assembly Instrumentation Library 
Runtime Assembly Instrumentation Library » rail » Reflect » Method.cs
/*
  The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
  you may not use this file except in compliance with the License. You may obtain a copy of the License at 
  http://www.mozilla.org/MPL/ 
  Software distributed under the License is distributed on an "AS IS" basis, 
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language 
  governing rights and limitations under the License. 

  The Original Code is RAIL(Runtime Assembly Instrumentation Library) Alpha Version.
  The Initial Developer of the Original Code is University of Coimbra,
  Computer Science Department, Dependable Systems Group. Copyright (C) University of Coimbra. 
  All Rights Reserved.  
*/
using System;
using System.Reflection;
using System.Reflection.Emit;
using Rail.MSIL;
using Rail.Transformation;
using Mono.PEToolkit;
using Mono.PEToolkit.Metadata;
using System.Runtime.InteropServices;
using System.Collections;

namespace Rail.Reflect{

  /// <summary>
  /// 
  /// </summary>
  public class RPInvokeInfo
  {
    private string dllName;
    private string entryName;
    private CallingConvention nativeCallConv;
    private CharSet nativeCharSet;

    /// <summary>
    /// 
    /// </summary>
    public string DLLName
    {
      get
      {
        return this.dllName;
      }
      set
      {
        this.dllName = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public string EntryName
    {
      get
      {
        return this.entryName;
      }
      set
      {
        this.entryName = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public CallingConvention NativeCallingConvention
    {
      get
      {
        return this.nativeCallConv;
      }
      set
      {
        this.nativeCallConv = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public CharSet NativeCharSet
    {
      get
      {
        return this.nativeCharSet;
      }
      set
      {
        this.nativeCharSet = value;
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="dllName"></param>
    /// <param name="flags"></param>
    internal RPInvokeInfo(string dllName,string entryName, int flags)
    {
      this.dllName = dllName;
      this.entryName = entryName;
      PInvokeAttributes pia = (PInvokeAttributes)flags;
      int mask = (int)(pia & PInvokeAttributes.CharSetMask);
      if (mask == (int)PInvokeAttributes.CharSetAnsi) 
      {
        this.nativeCharSet = CharSet.Ansi;
      } 
      else if (mask == (int)PInvokeAttributes.CharSetUnicode) 
      {
        this.nativeCharSet = CharSet.Unicode;
      } 
      else if (mask == (int)PInvokeAttributes.CharSetAuto) 
      {
        this.nativeCharSet = CharSet.Auto;
      } 
      else 
      {
        this.nativeCharSet = CharSet.None;
      }

      mask = (int)(pia & PInvokeAttributes.CallConvMask);
      if (mask == (int)PInvokeAttributes.CallConvWinapi) 
      {
        this.nativeCallConv = CallingConvention.Winapi;
      } 
      else if (mask == (int)PInvokeAttributes.CallConvCdecl) 
      {
        this.nativeCallConv = CallingConvention.Cdecl;
      } 
      else if (mask == (int)PInvokeAttributes.CallConvStdcall) 
      {
        this.nativeCallConv = CallingConvention.StdCall;
      } 
      else if (mask == (int)PInvokeAttributes.CallConvThiscall) 
      {
        this.nativeCallConv = CallingConvention.ThisCall;
      } 
      else if (mask == (int)PInvokeAttributes.CallConvFastcall) 
      {
        this.nativeCallConv = CallingConvention.FastCall;
      }  
    }

    internal RPInvokeInfo(string dllName, string entryName, CallingConvention cc, CharSet cs)
    {
      this.dllName = dllName;
      this.entryName = entryName;
      this.nativeCallConv = cc;
      this.nativeCharSet = cs;
    }
  }

  /// <summary>
  /// Contains functionality that is common to methods and constructors
  /// </summary>
  public abstract class RMethodBase : RMember 
  {
    #region fields
    /// <summary>
    /// 
    /// </summary>
    protected MethodAttributes attributes;
    /// <summary>
    /// 
    /// </summary>
    protected CallingConventions callingConventions;
    /// <summary>
    /// 
    /// </summary>
    protected MethodImplAttributes methodImplementationFlags;
    /// <summary>
    /// 
    /// </summary>
    protected int parametersCount;
    /// <summary>
    /// 
    /// </summary>
    protected RParameter[] parameters;
    /// <summary>
    /// 
    /// </summary>
    protected RPInvokeInfo pInvoke;
    /// <summary>
    /// The override information object
    /// </summary>
    protected ArrayList overridedMethod;

    internal RModule GlobalSetModule;
    #endregion

    /// <summary>
    /// Check it this is the implementation of a virtual method
    /// </summary>
    public bool IsOverrideBody
    {
      get
      {
        if (this.overridedMethod != null && this.overridedMethod.Count > 0)
          return true;
        return false;
      }
    }

    /// <summary>
    /// Get the declarion of the method or the oveerided method
    /// </summary>
    public virtual RMethodBase [] OverridedMethod
    {
      get
      {
        if (IsOverrideBody)
        {
          RMethodBase [] rMethod = new RMethodBase[this.overridedMethod.Count];
          int i = 0;
          foreach (RMethodBase rmb in this.overridedMethod)
          {
            rMethod[i] = rmb;
            ++i;
          }
          return rMethod;
        }
        else
          return null;
      }
    }

    /// <summary>
    /// Sets the method declaration
    /// </summary>
    /// <param name="overrideM">The method to override</param>
    /// <returns>The method to override</returns>
    internal void AddOverride(RMethodBase overrideM)
    {
      if (this.overridedMethod==null)
        this.overridedMethod =  new ArrayList();
      this.overridedMethod.Add(overrideM);
    }
    
    /// <summary>
    /// Verify if the method is PInvoke
    /// </summary>
    public bool IsPInvoke
    {
      get
      {
        return (this.Attributes & MethodAttributes.PinvokeImpl) != 0;
      }
    }
    /// <summary>
    /// Get the RPInvokeInfo object
    /// </summary>
    public virtual RPInvokeInfo PInvokeInfo
    {
      get
      {
        return this.pInvoke;
      }
      set
      {
        throw new NotSupportedException();
      }
    }
    /// <summary>
    /// 
    /// </summary>
    public virtual MethodAttributes Attributes 
    {
      get 
      {
        return this.attributes;
      }
      set 
      {
        throw new NotSupportedException();
      }
    }

    /// <summary>
    /// Gets and sets the CallingConventions for the method
    /// </summary>
    public virtual CallingConventions CallingConventions 
    {
      get 
      {
        return this.callingConventions;
      }
      set 
      {
        throw new NotSupportedException();
      }
    }

    /// <summary>
    /// Gets the implementation attributes of this method. (Managed, IL, native,
    /// runtime, etc)
    /// </summary>
    public virtual MethodImplAttributes MethodImplementationFlags 
    {
      get 
      {
        return this.methodImplementationFlags;
      }
      set
      {
        this.methodImplementationFlags = value;
      }
    }

    /// <summary>
    /// Gets the total number of parameters of this method.
    /// </summary>
    public int ParametersCount 
    {
      get 
      {
        if (this.parameters!=null) 
          return this.parameters.Length;
        else
          return 0;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    internal void OrderParameters()
    {
      if (this.parameters!=null)
        if (this.parameters.Length>0)
        {
          
          for (int i = 0; i < this.parameters.Length; i++)
          {
            if (this.parameters[i].Sequence==0 &&
              (this.attributes & MethodAttributes.Static)==0)              
              throw new InvalidOperationException();
          }
          RParameter [] tmp = new RParameter[this.parameters.Length];
          for (int i = 0; i < tmp.Length; i++)
          {
            if ((this.attributes & MethodAttributes.Static)==0)  
              tmp[this.parameters[i].Sequence-1] = this.parameters[i];
            else
              tmp[this.parameters[i].Sequence] = this.parameters[i];
          }
          this.parameters = tmp;
        }
    }

    /// <summary>
    ///  Gets a parameter
    /// </summary>
    /// <param name="pos"></param>
    /// <returns></returns>
    public RParameter GetParameter(int pos) 
    {
      if (this.parameters==null) return null;

      OrderParameters();

      for (int i = 0; i < this.parameters.Length; i++)
      {
        if (this.parameters[i].Sequence==0 && 
          (this.attributes & MethodAttributes.Static)==0)
          throw new InvalidOperationException();
      }

      if ((this.attributes & MethodAttributes.Static)!=0)
        return this.parameters[pos];
      else if (pos>0)
        return this.parameters[pos-1];
      return null;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public RParameter[] GetParameters() 
    {
      
      if (this.parameters == null)
      {
        this.parameters = new RParameter[0];
        this.parametersCount = 0;
      }
      else if (this.parameters.Length>0)
      {
        OrderParameters();
      }
      return this.parameters;
    }

    /// <summary>
    /// Override of the Equals method
    /// </summary>
    /// <param name="obj">The object to compare</param>
    /// <returns>True if the objects match</returns>
    public override bool Equals(object obj)
    {
      return base.Equals (obj);
    }

    /// <summary>
    /// Override of GetHashCode Method
    /// </summary>
    /// <returns>The HashCode</returns>
    public override int GetHashCode()
    {
      return base.GetHashCode ();
    }


  }


  /// <summary>
  /// The RAIL representation of a method
  /// </summary>
  public abstract class RMethod : RMethodBase 
  {
    #region fields
    /// <summary>
    /// The return type of the method
    /// </summary>
    protected RReturnType returnType;
    #endregion
    
    /// <summary>
    /// Gets or Sets the return type of the method
    /// </summary>
    public virtual RReturnType ReturnType 
    {
      get 
      {
        return this.returnType;
      }
      set 
      {
        throw new NotSupportedException();
      }
    }

  }
  

  /// <summary>
  /// Represenets a method reference in the assembly
  /// </summary>
  public sealed class RMethodRef : RMethod 
  {
    #region fields
    /// <summary>
    /// 
    /// </summary>
    private RAssemblyDef rad;
    #endregion

    /// <summary>
    /// 
    /// </summary>
    internal void SetAttributeStatic()
    {
      this.attributes = MethodAttributes.Static | MethodAttributes.Public;
    }
    /// <summary>
    /// Create a new RMethodRef
    /// </summary>
    /// <param name="mi">The original MethodInfo object</param>
    /// <param name="rad">The RAssemblyDef object were to create the reference</param>
    public RMethodRef(MethodInfo mi,RAssemblyDef rad) 
    {
      this.rad = rad;
      this.attributes = mi.Attributes;
      this.callingConventions = mi.CallingConvention;
      this.declaringType = rad.GetRTypeRef(mi.DeclaringType);
      this.memberType = mi.MemberType;
      //ROTOR
      
      this.methodImplementationFlags = (MethodImplAttributes)mi.GetMethodImplementationFlags();
      this.name = mi.Name;

      //parameters
      ParameterInfo [] pi = mi.GetParameters();
      if (pi!=null)
      {
        this.parameters = new RParameter[pi.Length];
        int i = 0;
        foreach (ParameterInfo paramInfo in pi)
        {
          this.parameters[i] = new RParameter(paramInfo,rad,this.attributes);
          i++;
        }
      }
      this.returnType = new RReturnType(mi.ReturnType,rad);
      
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="name"></param>
    /// <param name="retType"></param>
    /// <param name="declaringType"></param>
    /// <param name="pi"></param>
    /// <param name="rad"></param>
    public RMethodRef(string name,RType retType,RType declaringType,RParameter [] pi, RAssemblyDef rad) 
    {
      this.attributes = (MethodAttributes)0;
      this.rad = rad;
      this.memberType = MemberTypes.Method;
      this.callingConventions = (System.Reflection.CallingConventions)0;
      this.methodImplementationFlags = (System.Reflection.MethodImplAttributes)0;
      this.parameters = pi;
      this.declaringType = declaringType;
      this.name = name;
      this.parametersCount = pi.Length;
      this.returnType = new RReturnType(retType);
    }

  }

  /// <summary>
  /// 
  /// </summary>
  public sealed class RMethodDef : RMethod, IVisitable, ICustomAttributeOwnerDef 
  {  
    #region fields
    /// <summary>
    /// 
    /// </summary>
    private MSIL.MethodBody methodBody;
    #endregion

    /// <summary>
    /// 
    /// </summary>
    public RMethodDef() 
    {
      this.memberType = MemberTypes.Method;
    }
    

    /// <summary>
    /// Sets the method declaration
    /// </summary>
    /// <param name="overrideM">The method to override</param>
    /// <returns>The method to override</returns>
    public new void AddOverride(RMethodBase overrideM)
    {
      if (this.overridedMethod==null)
        this.overridedMethod =  new ArrayList();
      this.overridedMethod.Add(overrideM);
    }

    /// <summary>
    /// Remove the override info
    /// </summary>
    public void RemoveOverride(RMethodBase overrideM)
    {
      this.overridedMethod.Remove(overrideM);
    }
    /// <summary>
    /// Get or set the RPInvokeInfo object
    /// </summary>
    public override RPInvokeInfo PInvokeInfo
    {
      set
      {
        this.pInvoke = value;
      }
    }

    /// <summary>
    /// Create a new PInvokeObject for the methos
    /// </summary>
    /// <param name="dllName">The name of the invoked dll</param>
    /// <param name="entryName"></param>
    /// <param name="cc"></param>
    /// <param name="cs"></param>
    /// <returns>The created object</returns>
    public RPInvokeInfo DefinePInvokeInfo(string dllName,string entryName, CallingConvention cc, CharSet cs)
    {
      this.pInvoke = new RPInvokeInfo(dllName,entryName, cc, cs);
      return this.pInvoke;
    }
    /// <summary>
    /// Create a new PInvokeObject for the methos
    /// </summary>
    /// <param name="dllName">The name of the invoked dll</param>
    /// <param name="entryName"></param>
    /// <param name="flags">The flags for CallingConventios and CharSet</param>
    /// <returns>The created object</returns>
    internal RPInvokeInfo DefinePInvokeInfo(string dllName, string entryName, int flags)
    {
      this.pInvoke = new RPInvokeInfo(dllName, entryName, flags);
      return this.pInvoke;
    }
    /// <summary>
    /// 
    /// </summary>
    public override RReturnType ReturnType 
    {      
      set 
      {
        this.returnType = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public override MethodAttributes Attributes 
    {
      set 
      {
        this.attributes = value;
      }
    }

    #region MethodBody
    /// <summary>
    /// 
    /// </summary>
    public MSIL.MethodBody MethodBody 
    {
      get 
      {
        return this.methodBody;
      }
      set
      {
        this.methodBody = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="methodIL"></param>
    /// <param name="pe"></param>
    /// <returns></returns>
    internal MSIL.MethodBody DefineMethodBody(MethodIL methodIL,Image pe)  
    {  
      if (this.declaringType!=null)
      {
        if (methodIL != null && 
          (this.declaringType.Attributes & TypeAttributes.Interface) != TypeAttributes.Interface)
        {
          MethodLoadContext mlc 
            = new MethodLoadContext(pe,(RAssemblyDef)this.declaringType.Assembly);
          this.methodBody = new MSIL.MethodBody(mlc,methodIL,this,pe);
          return this.methodBody;
        }
      }
      else
      {
        //Is a Global Method
        if (methodIL != null)
        {
          MethodLoadContext mlc 
            = new MethodLoadContext(pe,(RAssemblyDef)this.GlobalSetModule.Assembly);
          this.methodBody = new MSIL.MethodBody(mlc,methodIL,this,pe);
          return this.methodBody;
        }
      }
      return null;
    }

    /// <summary>
    /// Create the MethodBody object for this method
    /// </summary>
    /// <returns>The MethodBody object created</returns>
    public MSIL.MethodBody DefineMethodBody()  
    {
      this.methodBody = new MSIL.MethodBody(this);
      return this.methodBody;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="tr"></param>
    internal void Write(TypeResolver tr)
    {
      MethodBuilder mBuilder = (MethodBuilder)tr.ResolveRMethod(this);
      if (this.MethodBody!=null && (this.declaringType.Attributes & TypeAttributes.Interface) != TypeAttributes.Interface)
      {
        ILGenerator ilGen = mBuilder.GetILGenerator();
        this.MethodBody.Write(ilGen,tr);
      }
    }
    #endregion

    /// <summary>
    /// Gets and sets the CallingConventions for the method
    /// </summary>
    public override CallingConventions CallingConventions 
    {
      set 
      {
        this.callingConventions = value;
      }
    }


    /// <summary>
    /// Gets or sets the name of this member.
    /// </summary>
    public override String Name 
    {
      set 
      {
        this.name = value;
      }
    }
    
    #region Parameters manipulation
    /// <summary>
    /// The parameter is removed and the indexes of the remaining 
    /// parameters are adjusted.
    /// 
    /// TODO: What if the parameter is refered in the code?
    /// </summary>
    /// <param name="index"></param>
    public RParameter RemoveParameter(int index) 
    {
      RParameter [] temp = null;
      RParameter retVal = null;
      if (this.parameters!=null)
        if (this.parameters.Length>=index)
        {
          temp = new RParameter[this.parameters.Length-1];
          int j=0;
          for (int i=0; i<this.parameters.Length;i++)
          {
            if ((this.attributes & MethodAttributes.Static)==0)            
            {
              if ((i+1)!=index)
              {
                temp[j] = this.parameters[i];
                temp[j].Sequence = j+1;
                j++;
              }
              else 
              {
                retVal = this.parameters[i];
              }
            }
            else
            {
              if (i!=index)
              {
                temp[j] = this.parameters[i];
                temp[j].Sequence = j;
                j++;
              }
              else 
              {
                retVal = this.parameters[i];
              }
            }
          }
          this.parameters = temp;
        }
      return retVal;
    }

    /// <summary>
    /// Adds the given parameter to the end of the list, returning 
    /// its position.
    /// </summary>
    /// <param name="parameter"></param>
    public int AddParameter(RParameter parameter) 
    {
      if (this.parameters!=null)
      {
        RParameter [] rpr = new RParameter[this.parameters.Length+1];
        for (int i = 0; i<this.parameters.Length;i++)
          rpr[i] = this.parameters[i];
        rpr[this.parameters.Length] = parameter;
        if ((this.attributes & MethodAttributes.Static)!=0)
          rpr[this.parameters.Length].Sequence = this.parameters.Length;
        else
          rpr[this.parameters.Length].Sequence = this.parameters.Length + 1;
        this.parameters = rpr;
        return rpr[this.parameters.Length-1].Sequence;
      }
      else
      {
        this.parameters = new RParameter[1];
        this.parameters[0] = parameter;
        if ((this.attributes & MethodAttributes.Static)!=0)
          this.parameters[0].Sequence = 0;
        else
          this.parameters[0].Sequence = 1;
        return this.parameters[0].Sequence;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="position"></param>
    /// <param name="parameter"></param>
    public void SetParameter(int position, RParameter parameter) 
    {
      //TODO:Check this functionality
      InsertParameter(position,parameter);
    }

    /// <summary>
    /// The parameters with indexes equal or greater than position are 
    /// shifted one place up and the given parameter is inserted. 
    /// </summary>
    /// <param name="parameter"></param>
    /// <param name="position"></param>
    public void InsertParameter(int position, RParameter parameter) 
    {
      RParameter [] temp = null;
      if (this.parameters!=null)
        if (this.parameters.Length>position)
        {
          temp = new RParameter[this.parameters.Length+1];
          if ((this.attributes & MethodAttributes.Static) == 0)
          {
            for (int i=0; i<this.parameters.Length;i++)
              if ((i+1)<position) 
              {
                temp[i] = this.parameters[i];
                temp[i].Sequence = i+1;
              }
              else if ((i+1)>position) 
              {
                temp[i+1] = this.parameters[i];
                temp[i+1].Sequence = i+2;
              }
              else 
              {
                temp[i] = parameter;
                temp[i].Sequence = i+1;
                temp[i+1] = this.parameters[i];
                temp[i+1].Sequence = i+2;
              }
          }
          else
          {
            for (int i=0; i<this.parameters.Length;i++)
              if (i<position) 
              {
                temp[i] = this.parameters[i];
                temp[i].Sequence = i;
              }
              else if (i>position) 
              {
                temp[i+1] = this.parameters[i];
                temp[i+1].Sequence = i+1;
              }
              else 
              {
                temp[i] = parameter;
                temp[i].Sequence = i;
                temp[i+1] = this.parameters[i];
                temp[i+1].Sequence = i+1;
              }
          }
          this.parameters = temp;
        }
    }
    #endregion

    #region Custom Attributes
    /// <summary>
    /// 
    /// </summary>
    /// <param name="attribute"></param>
    public void AddCustomAttributtes(RCustomAttrib attribute) 
    {
      InternalAddCustomAttributtes(attribute);
    }

    /// <summary>
    /// TODO: How should the attribute be specified?
    /// </summary>
    /// <param name="attribute"></param>
    public void RemoveCustomAttribute(RCustomAttrib attribute) 
    {
      InternalRemoveCustomAttribute(attribute);
    }

    #endregion
    /// <summary>
    /// Visits this method.
    /// </summary>
    /// <remarks>
    /// A method has no subelements    
    /// </remarks>
    /// <param name="v">The visitor to be used.</param>
    public void Accept(Visitor v) 
    {
      // We could call directly v.VisitMethod(this), but this way is more flexible
      // In the future, the Walker classes may be used for more than just choosing
      // the traversal order.
      v.GetWalker().WalkMethod(this);
    }
  }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.