Пример #1
0
        protected CodeExpression GenerateSingleton(CodeTypeDeclaration @class, CodeTypeReference type, string fieldName, CodeExpression initializer)
        {
            var cctor = Declare.Constructor( ).Static( )
                        .AddComment(SingletonBeforeFieldInitComment);

            if (CodeDomProvider.Supports(GeneratorSupport.NestedTypes))
            {
                var lazyType = Declare.NestedClass(fieldName).Private( ).Static( )
                               .AddTo(@class);

                cctor.AddTo(lazyType);

                Declare.Field(type, SingletonFieldName).Internal( ).Static( )
                .Initialize(initializer)
                .AddTo(lazyType);

                return(Code.Type(fieldName).Local( )
                       .Static( )
                       .Field(SingletonFieldName));
            }
            else
            {
                Declare.Field(type, fieldName).Private( ).Static( )
                .Initialize(initializer)
                .AddTo(@class);

                if ([email protected] <CodeTypeConstructor> ( ).Any( ))
                {
                    @class.Members.Add(cctor);
                }

                return(Code.Static( ).Field(fieldName));
            }
        }