Microsoft.Zing.Splicer.ExtendMethodConstructor C# (CSharp) Méthode

ExtendMethodConstructor() private méthode

private ExtendMethodConstructor ( Class newClass, ZMethod zMethod ) : void
newClass Class
zMethod ZMethod
Résultat void
        private void ExtendMethodConstructor(Class newClass, ZMethod zMethod)
        {
            int numAddedParams = 0;

            // Duplicate our basic constructor so we can make an extended version
            System.Compiler.Duplicator dup = new System.Compiler.Duplicator(null, null);
            InstanceInitializer ctor = dup.VisitInstanceInitializer((InstanceInitializer)newClass.Members[0]);

            if (!zMethod.IsStatic)
            {
                // For instance methods, add a "This" parameter to the constructor
                ctor.Parameters.Add(new Parameter(Identifier.For("This"), new TypeExpression(Identifier.For("Pointer"))));

                // this.This = This;
                ctor.Body.Statements.Add(
                    new ExpressionStatement(
                        new AssignmentExpression(
                            new AssignmentStatement(
                                new QualifiedIdentifier(new This(), Identifier.For("This")),
                                Identifier.For("This"))))
                );

                numAddedParams++;
            }

            for (int i = 0, n = zMethod.Parameters.Count; i < n; i++)
            {
                TypeNode paramType;
                Parameter param = zMethod.Parameters[i];

                if (param == null || param.Type == null)
                    continue;

                if (param.IsOut)
                    continue;

                if (GetTypeClassification(param.Type) == TypeClassification.Heap)
                    paramType = this.ZingPtrType;
                else if (!IsPredefinedType(param.Type))
                    paramType = new TypeExpression(new QualifiedIdentifier(
                        new Identifier("Application"), param.Type.Name));
                else
                    paramType = param.Type;

                ctor.Parameters.Add(new Parameter(param.Name, paramType));
                // inputs.foo = foo;
                ctor.Body.Statements.Add(
                    new ExpressionStatement(
                        new AssignmentExpression(
                            new AssignmentStatement(
                                new QualifiedIdentifier(Identifier.For("inputs"), param.Name),
                                param.Name)))
                    );

                numAddedParams++;
            }

            // If we didn't actually add any parameters, then the basic constructor is
            // all that we need. Only add the new constructor if it's different.
            if (numAddedParams > 0)
            {
                newClass.Members.Add(ctor);
                ctor.DeclaringType = newClass;
            }
        }