コード例 #1
0
 /// <summary>
 ///     Appends the content.
 /// </summary>
 /// <param name="codeNamespace">The code namespace.</param>
 /// <param name="container">The container.</param>
 private static void AppendContent(CodeNamespace codeNamespace, SettingsContainer container)
 {
     AppendClass(codeNamespace, container);
 }
コード例 #2
0
        /// <summary>
        ///     Adds the singleton instance.
        /// </summary>
        /// <param name="codeTypeDeclaration">The code type declaration.</param>
        /// <param name="container">The container.</param>
        private static void AddSingletonInstance(CodeTypeDeclaration codeTypeDeclaration, SettingsContainer container)
        {
            var settingsReference      = new CodeTypeReference(codeTypeDeclaration.Name);
            var singletonField         = new CodeMemberField(settingsReference, "defaultInstance");
            var newInstanceExpression  = new CodeObjectCreateExpression(container.Name);
            var synchronizedExpression = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(SettingsBaseReference), "Synchronized", newInstanceExpression);

            singletonField.Attributes     = MemberAttributes.Private | MemberAttributes.Static;
            singletonField.InitExpression = new CodeCastExpression(codeTypeDeclaration.Name, synchronizedExpression);

            codeTypeDeclaration.Members.Add(singletonField);

            var codeMemberProperty = new CodeMemberProperty();

            codeMemberProperty.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            codeMemberProperty.Name       = "Default";
            codeMemberProperty.Type       = settingsReference;
            codeMemberProperty.HasGet     = true;
            codeMemberProperty.HasSet     = false;

            var codeFieldReferenceExpression = new CodeFieldReferenceExpression();

            codeFieldReferenceExpression.FieldName = singletonField.Name;

            codeMemberProperty.GetStatements.Add(new CodeMethodReturnStatement(codeFieldReferenceExpression));
            codeTypeDeclaration.Members.Add(codeMemberProperty);
        }