예제 #1
0
        private void ProcessConstructors(pMixinGeneratorPipelineState manager, CodeGeneratorProxy requirementInterface)
        {
            bool hasANonParamaterlessConstructor =
                manager.CurrentpMixinAttribute.Mixin.GetConstructors()
                .Any(c => !c.Parameters.Any());

            if (hasANonParamaterlessConstructor &&
                !manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsAbstract&&
                !manager.CurrentpMixinAttribute.ExplicitlyInitializeMixin)
            {
                return;
            }


            //Update ExplicitlyInitializeMixin - Changed mind, user has to always set
            //ExplicitlyInitializeMixin to true even if mixin does not have parameterless constructor.
            //They could be using a custom MixinActivator that uses DI
            //manager.CurrentpMixinAttribute.ExplicitlyInitializeMixin = true;
            if (!manager.CurrentpMixinAttribute.ExplicitlyInitializeMixin)
            {
                return;
            }


            var requiredMixinWrapperType =
                (manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsAbstract)
                    ? GenerateMixinSpecificAutoGeneratedClass.GetFullNameForChildType(
                    manager,
                    GenerateAbstractMixinMembersWrapperClass.GetWrapperClassName(manager))
                    : manager.CurrentpMixinAttribute.Mixin.GetOriginalFullNameWithGlobal();

            //generate the mixin initialization method
            requirementInterface.CreateMethod(
                string.Empty, //no modifier for interface member
                requiredMixinWrapperType,
                GetInitializationMethod(manager),
                new List <KeyValuePair <string, string> >(),
                "");
        }
        /// <summary>
        /// Generate code similar to:
        /// <code>
        /// <![CDATA[
        ///     AbstractWrapper _host;
        ///
        ///     public TargetWrapper(IMixinRequirements host){ _host = host; }
        /// ]]>
        /// </code>
        /// </summary>
        private void CreateConstructorAndRequirementsInterfaceDataMember(
            pMixinGeneratorPipelineState manager, ICodeGeneratorProxy wrapperClass)
        {
            var mixinInstanceType = "";

            #region Assign Mixin Instance Type
            if (manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsAbstract)
            {
                //Abstract Mixin Wrapper is in a different namespace, so we need the fullname
                mixinInstanceType =
                    ExternalGeneratedNamespaceHelper.GenerateChildClassFullName(
                        manager,
                        GenerateAbstractMixinMembersWrapperClass.GetWrapperClassName(manager));
            }
            else if (manager.CurrentMixinMembers.Any(x => x.Member.IsProtected))
            {
                //Abstract Mixin Wrapper is in the same namespace
                mixinInstanceType = GenerateAbstractMixinMembersWrapperClass.GetWrapperClassName(manager);
            }
            else
            {
                mixinInstanceType = manager.CurrentpMixinAttribute.Mixin.GetOriginalFullNameWithGlobal();
            }
            #endregion


            //add data member
            wrapperClass.CreateDataMember(

                /* Can't remember why it was necessary to have a modifier other than public
                 * manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsPublic
                 *  ? "public"
                 *  : manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsInternal
                 *      ? "internal"
                 *      : "private"
                 */"public"
                + " readonly",
                mixinInstanceType,
                MixinInstanceDataMemberName);

            string mixinInstanceInitialization;

            #region Create Mixin Instance (InitializeMixin / TryActivateMixin
            if (manager.CurrentpMixinAttribute.ExplicitlyInitializeMixin)
            {
                mixinInstanceInitialization =
                    string.Format("(({0}){1}).{2}();",
                                  AddMixinConstructorRequirementDependency
                                  .GetMixinConstructorRequirement(manager),
                                  MixinInstanceDataMemberName.Replace("_", ""),
                                  "InitializeMixin");
            }
            else if (manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsSealed)
            {
                mixinInstanceInitialization =
                    string.Format("base.TryActivateMixin<{0}>();",
                                  manager.CurrentpMixinAttribute.Mixin.GetOriginalFullNameWithGlobal());
            }
            else if (manager.CurrentpMixinAttribute.Mixin.GetDefinition().IsAbstract)
            {
                mixinInstanceInitialization =
                    string.Format("base.TryActivateMixin<{0}>({1});",
                                  mixinInstanceType,
                                  MixinInstanceDataMemberName.Replace("_", ""));
            }
            else
            {
                mixinInstanceInitialization =
                    string.Format("base.TryActivateMixin<{0}>({1});",
                                  GenerateMixinSpecificAutoGeneratedClass.GetFullNameForChildType(
                                      manager,
                                      GenerateAbstractMixinMembersWrapperClass.GetWrapperClassName(manager)),
                                  MixinInstanceDataMemberName.Replace("_", ""));
            }
            #endregion


            var initializeBaseExpression =
                #region base.Initialize
                string.Format("base.Initialize( {0}, {1}, new global::{2}<global::{3}>{{ {4} }});",
                              MixinInstanceDataMemberName.Replace("_", ""),
                              MixinInstanceDataMemberName,
                              "System.Collections.Generic.List",
                              typeof(IMixinInterceptor),
                              string.Join(",",
                                          manager.CurrentpMixinAttribute.Interceptors
                                          .Select(x => x.GenerateActivationExpression())
                                          )
                              );
            #endregion

            wrapperClass.CreateConstructor(
                "public",
                new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>(
                    manager.CurrentMixinRequirementsInterface.EnsureStartsWith("global::"),
                    MixinInstanceDataMemberName.Replace("_", ""))
            },
                constructorInitializer: "",
                constructorBody:
                MixinInstanceDataMemberName + " = " +
                mixinInstanceInitialization +
                CreateConstructorInitializersForVirtualMemberFunctions(manager, wrapperClass) +
                initializeBaseExpression
                );
        }