Esempio n. 1
0
        private static async Task GenerateWrapperImplementations()
        {
            string baseDirectory = Path.GetFullPath(
                Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), @"..\..\..\.."));
            SolutionWithCodeAnalysis solution = await SolutionWithCodeAnalysis.OpenAsync(
                Path.Combine(baseDirectory, @"CSharpDom.sln"));

            ProjectWithCodeAnalysis project = solution.Projects.First(p => p.Syntax.Name == "CSharpDom");

            project.Lock();
            foreach (DocumentWithCodeAnalysis document in project.Documents
                     .Where(document => document.FullFilePath.Contains(@"CSharpDom\Wrappers\Internal"))
                     .OrderBy(document => document.FullFilePath))
            {
                document.IsLocked = true;
                bool isEdited = false;
                LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync();

                using (CodeAnalysisSettings.AllowEdits(loadedDocument))
                {
                    SealedClassWithCodeAnalysis @class = loadedDocument.Namespaces.First().Classes.SealedClasses.FirstOrDefault();
                    if (@class == null)
                    {
                        continue;
                    }

                    foreach (SealedClassPropertyWithCodeAnalysis property in @class.Properties
                             .Where(property => property.GetAccessor.Body.Statements.FirstOrDefault() is ThrowStatementWithCodeAnalysis)
                             .ToArray())
                    {
                        isEdited = true;
                        string propertyName = property.Name;
                        string fieldName    = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
                        SealedClassFieldWithCodeAnalysis field = new SealedClassFieldWithCodeAnalysis(
                            ClassMemberVisibilityModifier.Private,
                            new DelegateReferenceWithCodeAnalysis("Func", property.PropertyType),
                            fieldName);
                        @class.Fields.Fields.Add(field);
                        IList <IStatementWithCodeAnalysis> statements = property.GetAccessor.Body.Statements;
                        statements.Clear();
                        statements.Add(
                            StatementFactory.Return(ExpressionFactory.MethodCall(ExpressionFactory.Identifier(fieldName))));
                    }
                }

                if (isEdited)
                {
                    string sourceCode = loadedDocument.ToSourceCode();
                    File.WriteAllText(document.FullFilePath, sourceCode);
                }
            }
        }
Esempio n. 2
0
 internal SealedPartialClassFieldWithCodeAnalysis(SealedClassFieldWithCodeAnalysis field)
 {
     this.field = field;
     field.DeclaringTypeFunc = () => DeclaringType.Class;
 }