コード例 #1
0
ファイル: NETImport.cs プロジェクト: xiaoxiongnpu/NClass
 /// <summary>
 /// Adds the given methods to the given type.
 /// </summary>
 /// <param name="type">The entity to add the methods to.</param>
 /// <param name="methods">A list of methods to add.</param>
 private void AddMethods(CompositeType type, IEnumerable <NRMethod> methods)
 {
     foreach (NRMethod nrMethod in methods)
     {
         type.AddMethod().InitFromDeclaration(new NRMethodDeclaration(nrMethod));
     }
 }
コード例 #2
0
        /// <summary>
        /// Adds the given methods to the given type.
        /// </summary>
        private void AddMethods(CompositeType type, TypeDeclaration tp)
        {
            foreach (MethodDeclaration mp in tp.Descendants.OfType <MethodDeclaration>())
            {
                Method method = type.AddMethod();

                method.Name = mp.Name;

                method.Type           = mp.ReturnType.ToString();
                method.AccessModifier = mp.Modifiers.ToEnClass();

                CSharpArgumentList Arg = new CSharpArgumentList();

                foreach (ParameterDeclaration ichParameter in mp.Parameters)
                {
                    string defaultValue = string.Empty;
                    if (ichParameter.DefaultExpression is PrimitiveExpression)
                    {
                        PrimitiveExpression defaultExpression = (PrimitiveExpression)ichParameter.DefaultExpression;
                        defaultValue = defaultExpression.Value.ToString();
                    }

                    Arg.Add(ichParameter.Name, ichParameter.Type.ToString(), ichParameter.ParameterModifier.ToEnClass(), defaultValue);
                }

                method.ArgumentList = Arg;
            }
        }
コード例 #3
0
ファイル: NETImport.cs プロジェクト: immeraufdemhund/NClass
 /// <summary>
 ///     Adds the given methods to the given type.
 /// </summary>
 /// <param name="type">The entity to add the methods to.</param>
 /// <param name="methods">A list of methods to add.</param>
 private void AddMethods(CompositeType type, IEnumerable <NRMethod> methods)
 {
     foreach (var nrMethod in methods)
     {
         type.AddMethod().InitFromString(nrMethod.Declaration());
     }
 }
コード例 #4
0
ファイル: MembersDialog.cs プロジェクト: Veggie13/raptor
        private void toolNewMethod_Click(object sender, EventArgs e)
        {
            Method       method = parent.AddMethod();
            ListViewItem item   = AddOperationToList(method);

            if (method.Parent is ClassType)
            {
                method.raptorTab =
                    NClass.Core.ProjectCore.raptorUpdater.createMethod((method.Parent as ClassType).raptorTab,
                                                                       method.Name,
                                                                       method);
            }
            OnContentsChanged(EventArgs.Empty);
            item.Focused  = true;
            item.Selected = true;
            txtName.SelectAll();
            txtName.Focus();
        }
コード例 #5
0
        /// <summary>
        /// Adds the given operators to the given type.
        /// </summary>
        private void AddOperators(CompositeType type, TypeDeclaration tp)
        {
            foreach (OperatorDeclaration op in tp.Descendants.OfType <OperatorDeclaration>())
            {
                Method method = type.AddMethod();

                method.Name           = op.Name;
                method.Type           = op.ReturnType.ToString();
                method.AccessModifier = op.Modifiers.ToEnClass();

                CSharpArgumentList Arg = new CSharpArgumentList();

                foreach (ParameterDeclaration ichParameter in op.Parameters)
                {
                    Arg.Add(ichParameter.Name, ichParameter.Type.ToString(), ichParameter.ParameterModifier.ToEnClass(), ichParameter.DefaultExpression.ToString());
                }

                method.ArgumentList = Arg;
            }
        }
コード例 #6
0
        private void toolNewMethod_Click(object sender, EventArgs e)
        {
            Method method = parent.AddMethod();

            AddNewOperation(method);
        }
コード例 #7
0
        /// <summary>
        /// Reflects all operations within the type <paramref name="xType"/>. Reflected
        /// operations are added to <paramref name="xOpContainer"/>.
        /// </summary>
        /// <param name="xType">The operations are taken from this type.</param>
        /// <param name="xOpContainer">Reflected operations are added to this OperationContainer.</param>
        private void ReflectOperations(Type xType, CompositeType xOpContainer)
        {
            #region --- Properties

            PropertyInfo[] axProperties     = xType.GetProperties(xStandardBindingFlags);
            List <string>  astPropertyNames = new List <string>();
            foreach (PropertyInfo xProperty in axProperties)
            {
                //Don't display derived Methods
                if (xProperty.DeclaringType != xType)
                {
                    continue;
                }

                astPropertyNames.Add(xProperty.Name);
                StringBuilder stDeclaration = new StringBuilder();
                //The access modifier for a property isn't stored at the property.
                //We have to use the access modifier from the corresponding get_XXX /
                //set_XXX Method.
                //Well - that's not the whole truth... It's possible to create a
                //public property with a private set method. But there could be also
                //a private property with a public get method. There's no way to get
                //this information back from the assembly.
                //Solution (for now): Take the first found access modifier as the
                // property access modifier.
                ParameterInfo[] axIndexParameter = xProperty.GetIndexParameters();
                //get_XXX and set_XXX might be overloaded, so we have to specify the
                //index parameters as well.
                Type[] axGetIndexParamTypes = new Type[axIndexParameter.Length];
                Type[] axSetIndexParamTypes = new Type[axIndexParameter.Length + 1];
                for (int i = 0; i < axIndexParameter.Length; i++)
                {
                    axGetIndexParamTypes[i] = axIndexParameter[i].ParameterType;
                    axSetIndexParamTypes[i] = axIndexParameter[i].ParameterType;
                }
                axSetIndexParamTypes[axSetIndexParamTypes.Length - 1] = xProperty.PropertyType;
                string     stGetMethodName = xProperty.Name.Insert(xProperty.Name.LastIndexOf('.') + 1, "get_");
                string     stSetMethodName = xProperty.Name.Insert(xProperty.Name.LastIndexOf('.') + 1, "set_");
                MethodInfo xGetMethod      = xType.GetMethod(stGetMethodName, xStandardBindingFlags, null, axGetIndexParamTypes, null); //, xStandardBindingFlags);
                MethodInfo xSetMethod      = xType.GetMethod(stSetMethodName, xStandardBindingFlags, null, axSetIndexParamTypes, null); //, xStandardBindingFlags);

                //If one of the access methods (get_XXX or set_XXX) should be importet
                //import the property.
                if (!((xProperty.CanRead && xImportSettings.CheckImportProperty(xGetMethod)) ||
                      (xProperty.CanWrite && xImportSettings.CheckImportProperty(xSetMethod))))
                {
                    continue;
                }
                if (!(xOpContainer is InterfaceType))
                {
                    if (xProperty.CanRead)
                    {
                        stDeclaration.Append(GetMethodAccessModifierString(xGetMethod));
                        stDeclaration.AppendFormat(" {0}", GetOperationModifierString(xGetMethod));
                        ChangeOperationModifierIfOverwritten(xType, xGetMethod, stDeclaration);
                    }
                    else
                    {
                        stDeclaration.Append(GetMethodAccessModifierString(xSetMethod));
                        stDeclaration.AppendFormat(" {0}", GetOperationModifierString(xSetMethod));
                        ChangeOperationModifierIfOverwritten(xType, xSetMethod, stDeclaration);
                    }
                }
                stDeclaration.AppendFormat(" {0}", GetTypeName(xProperty.PropertyType));
                //Is this an Item-property (public int this[int i])
                if (axIndexParameter.Length > 0)
                {
                    stDeclaration.Append(" this[");
                    foreach (ParameterInfo xParameter in axIndexParameter)
                    {
                        stDeclaration.AppendFormat("{0} ", GetTypeName(xParameter.ParameterType));
                        stDeclaration.AppendFormat("{0}, ", xParameter.Name);
                    }
                    //Get rid of the last ", "
                    stDeclaration.Length -= 2;
                    stDeclaration.Append("]");
                }
                else
                {
                    stDeclaration.AppendFormat(" {0}", xProperty.Name);
                }
                stDeclaration.AppendFormat(" {0}", "{");
                if (xProperty.CanRead)
                {
                    stDeclaration.AppendFormat(" {0}", "get;");
                    if (xProperty.CanWrite)
                    {
                        if (!stDeclaration.ToString().StartsWith(GetMethodAccessModifierString(xSetMethod)) && !(xOpContainer is InterfaceType))
                        {
                            //! set has a different access modifier
                            stDeclaration.AppendFormat(" {0}", GetMethodAccessModifierString(xSetMethod));
                        }
                        stDeclaration.AppendFormat(" {0}", "set;");
                    }
                }
                else
                {
                    //! Only set, so don't care about access modifier
                    stDeclaration.AppendFormat(" {0}", "set;");
                }
                stDeclaration.AppendFormat(" {0}", "}");

                Property xNewProperty = xOpContainer.AddProperty();
                xNewProperty.InitFromString(stDeclaration.ToString());
            }

            #endregion

            #region --- Methods

            MethodInfo[] axMethods = xType.GetMethods(xStandardBindingFlags);
            foreach (MethodInfo xMethod in axMethods)
            {
                //Don't display derived Methods
                if (xMethod.DeclaringType != xType)
                {
                    continue;
                }

                if (!xImportSettings.CheckImportMethod(xMethod))
                {
                    continue;
                }

                //There are sometimes some magic methods like '<.ctor>b__0'. Those
                //methods are generated by the compiler and shouldn't be importet.
                //Those methods have an attribute CompilerGeneratedAttribute.
                if (xMethod.GetCustomAttributes(typeof(CompilerGeneratedAttribute), true).Length > 0)
                {
                    continue;
                }

                //We store the method name here so it is much easier to take care about operators
                string stMethodName    = xMethod.Name;
                bool   boAddReturnType = true;
                if (xMethod.IsSpecialName)
                {
                    //SpecialName means that this method is an automaticaly generated
                    //method. This can be get_XXX and set_XXX for properties or
                    //add_XXX and remove_XXX for events. There are also special name
                    //methods starting with op_ for operators.
                    if (!xMethod.Name.StartsWith("op_"))
                    {
                        continue;
                    }
                    //!xMethod.Name starts witch 'op_' and so it is an operator. We
                    //have to get the 'real' method name here.
                    if (dstOperatorMethodsMap.ContainsKey(stMethodName))
                    {
                        stMethodName = dstOperatorMethodsMap[stMethodName];
                    }

                    if (stMethodName == "op_Implicit")
                    {
                        stMethodName    = "implicit operator " + GetTypeName(xMethod.ReturnType);
                        boAddReturnType = false;
                    }
                    else if (stMethodName == "op_Explicit")
                    {
                        stMethodName    = "explicit operator " + GetTypeName(xMethod.ReturnType);
                        boAddReturnType = false;
                    }
                }

                Method        xNewMethod    = (Method)xOpContainer.AddMethod();
                StringBuilder stDeclaration = new StringBuilder();

                if (!(xOpContainer is InterfaceType))
                {
                    stDeclaration.AppendFormat("{0} ", GetMethodAccessModifierString(xMethod));
                    stDeclaration.AppendFormat("{0} ", GetOperationModifierString(xMethod));
                }

                ChangeOperationModifierIfOverwritten(xType, xMethod, stDeclaration);

                if (boAddReturnType)
                {
                    stDeclaration.AppendFormat("{0} ", GetTypeName(xMethod.ReturnType));
                }
                stDeclaration.AppendFormat("{0}", stMethodName);

                stDeclaration.AppendFormat("{0}", GetParameterDeclarationString(xMethod));

                xNewMethod.InitFromString(stDeclaration.ToString());
            }

            #endregion

            #region --- Constructors

            if (xOpContainer.SupportsConstuctors)
            {
                ConstructorInfo[] axConstructors = xType.GetConstructors(xStandardBindingFlags);
                foreach (ConstructorInfo xConstructor in axConstructors)
                {
                    if (!xImportSettings.CheckImportConstructor(xConstructor))
                    {
                        continue;
                    }
                    Method        xNewConstructor = xOpContainer.AddConstructor();
                    StringBuilder stDeclaration   = new StringBuilder();
                    stDeclaration.AppendFormat("{0} ", GetMethodAccessModifierString(xConstructor));
                    stDeclaration.AppendFormat("{0}", xConstructor.IsStatic ? "static " : "");
                    stDeclaration.Append(xOpContainer.Name); //xOpContainer.Name since the name of the class/struct/... isn't set yet

                    stDeclaration.AppendFormat("{0}", GetParameterDeclarationString(xConstructor));
                    xNewConstructor.InitFromString(stDeclaration.ToString());
                }
            }

            #endregion

            ReflectTypeBase(xType, (TypeBase)xOpContainer);
        }