Пример #1
0
        private SourceCode GetFields(SemanticModel semanticModel, HashSet <string> typeReferences,
                                     IEnumerable <FieldDeclarationSyntax> fieldsDeclarations)
        {
            var generatedFields = new SourceCode {
                MainPart = ""
            };

            foreach (var fieldDeclaration in fieldsDeclarations)
            {
                var variables = fieldDeclaration.Declaration.Variables;
                if (variables.Count != 1)
                {
                    throw new NotImplementedException();
                }
                var    variable      = variables.First();
                string identifier    = variable.Identifier.ValueText;
                var    fieldType     = fieldDeclaration.Declaration.Type;
                var    typeReference = TypeReferenceGenerator.GenerateTypeReference(fieldType, semanticModel);
                if (!typeReference.IsPredefined)
                {
                    typeReferences.Add(typeReference.Text);
                }

                bool   isStatic = HasStaticModifier(fieldDeclaration.Modifiers.ToList());
                string optionalStaticModifierText = isStatic ? "static " : "";

                var    fieldAccessModifier     = GetAccessModifier(fieldDeclaration.Modifiers.ToList());
                string generatedAccessModifier = fieldAccessModifier == AccessModifier.Public ? "public" : "private";

                string initializerText = "";
                if (variable.Initializer != null)
                {
                    string initializerValueText = ExpressionGenerator
                                                  .GenerateExpression(variable.Initializer.Value, semanticModel);
                    initializerText += " = " + initializerValueText;
                }

                string generatedField =
                    generatedAccessModifier + " " + optionalStaticModifierText + typeReference.Text + " "
                    + identifier + initializerText + ";\n";
                generatedFields.MainPart += "\n" + generatedField;
            }
            return(generatedFields);
        }
Пример #2
0
 private static EqualsValueClauseSyntax GetDefaultValue(IParameterSymbol parameter)
 => SyntaxFactory.EqualsValueClause(ExpressionGenerator.GenerateExpression(parameter.Type, parameter.ExplicitDefaultValue, canUseFieldReference: true));
Пример #3
0
 public void SetUp()
 {
     generator = new ConstructorExpressionGenerator(ConstructorSelectors.MostSpecific);
     generator.DependencyExpressionGenerating += (sender, args) => { args.Expression = generator.GenerateExpression(args.DependencyType); };
 }
 protected override SyntaxNode GenerateExpressionFromOptionalParameter(IParameterSymbol parameterSymbol)
 {
     return(ExpressionGenerator.GenerateExpression(parameterSymbol.Type, parameterSymbol.ExplicitDefaultValue, canUseFieldReference: true));
 }
Пример #5
0
 public void Test1()
 {
     var expression = ExpressionGenerator.GenerateExpression(model => model.Age, 15, Operator.GreaterThan);
 }