Constructor.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 » Constructor.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.Collections;

namespace Rail.Reflect{
  /// <summary>
  /// 
  /// </summary>
  public abstract class RConstructor : RMethodBase 
  {    

  }

  /// <summary>
  /// 
  /// </summary>
  public sealed class RConstructorRef : RConstructor 
  {
    #region fields
    /// <summary>
    /// 
    /// </summary>
    private RAssemblyDef rad;
    #endregion

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ci"></param>
    /// <param name="rad"></param>
    public RConstructorRef(ConstructorInfo ci, RAssemblyDef rad) 
    {
      this.attributes = ci.Attributes;
      this.rad = rad;
      this.callingConventions = ci.CallingConvention;
      this.declaringType = rad.GetRTypeRef(ci.DeclaringType);
      this.memberType = ci.MemberType;
      //ROTOR
      
      this.methodImplementationFlags = (MethodImplAttributes)0;
      this.name = ci.Name;
      ParameterInfo [] pi = ci.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.parametersCount = pi.Length;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="name"></param>
    /// <param name="retType"></param>
    /// <param name="declaringType"></param>
    /// <param name="pi"></param>
    /// <param name="rad"></param>
    public RConstructorRef(string name,RType retType,RType declaringType,RParameter [] pi, RAssemblyDef rad) 
    {
      this.attributes = (MethodAttributes)0;
      this.rad = rad;
      this.memberType = MemberTypes.Constructor;
      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;
    }

  }

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

    /// <summary>
    /// 
    /// </summary>
    internal RConstructorDef() 
    {
      this.attributes = (MethodAttributes)0;
      this.bindingFlags = (System.Reflection.BindingFlags)0;
      this.callingConventions = (System.Reflection.CallingConventions)0;
      this.declaringType = null;
      this.memberType = System.Reflection.MemberTypes.Constructor;
      this.methodBody = null;
      this.methodImplementationFlags = (System.Reflection.MethodImplAttributes)0;
      this.name = null;
      this.parameters = null;
      this.parametersCount = 0;
      
    }

    /// <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>
    /// Gets and sets the CallingConventions for the method
    /// </summary>
    public override CallingConventions CallingConventions 
    {
      set 
      {
        this.callingConventions = value;
      }
    }

    /// <summary>
    /// 
    /// </summary>
    public override MethodAttributes Attributes 
    {
      set 
      {
        this.attributes = 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+1;
        else
          rpr[this.parameters.Length].Sequence = this.parameters.Length;
        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 = 1;
        else
          this.parameters[0].Sequence = 0;
        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];
          for (int i=0; i<this.parameters.Length;i++)
          {
            if ((this.attributes & MethodAttributes.Static)==0)
            {
              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
            {
              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 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 (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
      {
        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>
    /// 
    /// </summary>
    /// <returns></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)
    {
      ConstructorBuilder cBuilder = (ConstructorBuilder)tr.ResolveRConstructor(this);
      if (this.methodBody != null)
      {
        ILGenerator ilGen = cBuilder.GetILGenerator();
        this.MethodBody.Write(ilGen,tr);
      }
    }
    #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 constructor.
    /// </summary>
    /// <remarks>
    /// A constructor has no subelements    
    /// </remarks>
    /// <param name="v">The visitor to be used.</param>
    public void Accept(Visitor v) 
    {      
      v.GetWalker().WalkConstructor(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.