Ejemplo n.º 1
0
 private void CreateRequirementsDataMember(
     ICodeGeneratorProxy codeGenerator,
     MixinLevelCodeGeneratorPipelineState manager)
 {
     codeGenerator.CreateDataMember(
         modifiers:
         "private readonly",
         dataMemberTypeFullName:
         manager.RequirementsInterface
         .GetFullTypeName().EnsureStartsWith("global::"),
         dataMemberName:
         RequirementsVariable);
 }
        private void CreateRequirementsDataMemberAndConstructor(
            ICodeGeneratorProxy wrapperClass, pMixinGeneratorPipelineState manager)
        {
            wrapperClass.CreateDataMember(
                "private readonly",
                manager.CurrentMixinRequirementsInterface.EnsureStartsWith("global::"),
                RequirementsVariable);

            if (!manager.CurrentpMixinAttribute.ExplicitlyInitializeMixin &&
                manager.CurrentpMixinAttribute.Mixin.GetConstructors()
                .Any(c => !c.Parameters.Any()))
            {
                //No explicit initialization, so just add a simple constructor
                //that takes a single parameter of type CurrentMixinRequirementsInterface

                wrapperClass.CreateConstructor(
                    "public",
                    new [] { new KeyValuePair <string, string>(
                                 manager.CurrentMixinRequirementsInterface.EnsureStartsWith("global::"),
                                 RequirementsVariable.Replace("_", "")) },
                    "",
                    RequirementsVariable + "= " + RequirementsVariable.Replace("_", "") + ";");

                return;
            }

            //Explicit initialization is needed, so wrap all existing constructors
            foreach (var constructor in manager.CurrentpMixinAttribute.Mixin.GetConstructors())
            {
                var updatedParameters =
                    constructor.Parameters.ToKeyValuePair();

                updatedParameters.Insert(0,
                                         new KeyValuePair <string, string>(
                                             manager.CurrentMixinRequirementsInterface.EnsureStartsWith("global::"),
                                             RequirementsVariable.Replace("_", "")));

                wrapperClass.CreateConstructor(
                    "public",
                    updatedParameters,
                    ": base(" + string.Join(",", constructor.Parameters.Select(p => p.Name)) + ")",
                    RequirementsVariable + "= " + RequirementsVariable.Replace("_", "") + ";");
            }
        }
        /// <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
                );
        }