public TemplateMemberGenerator(CodeGenerationContext context)
        {
            Context = context;

            if (!String.IsNullOrEmpty(context.Model.MemberTemplateFile))
            {
                string templateFile = context.Model.MemberTemplateFile;
                if (!Path.IsPathRooted(templateFile))
                    templateFile = Helper.FindFile(context.ModelFilePath, templateFile);

                if (templateFile == null || !File.Exists(templateFile))
                    throw new FileNotFoundException("Template file not found", context.Model.MemberTemplateFile);

                SupportedLanguage language;
                if (templateFile.EndsWith(".cs", StringComparison.InvariantCultureIgnoreCase))
                    language = SupportedLanguage.CSharp;
                else if (templateFile.EndsWith(".vb", StringComparison.InvariantCultureIgnoreCase))
                    language = SupportedLanguage.VBNet;
                else
                    throw new Exception("Only .cs and .vb files are supported for MemberTemplateFile.");

                using (StreamReader reader = new StreamReader(templateFile))
                {
                    IParser parser = ParserFactory.CreateParser(language, reader);
                    parser.Parse();

                    if (parser.Errors.Count > 0)
                        throw new Exception("Error detected parsing MemberTemplateFile.");

                    CodeDomVisitor visit = new CodeDomVisitor();

                    visit.VisitCompilationUnit(parser.CompilationUnit, null);

                    // Remove Unsed Namespaces
                    for (int i = visit.codeCompileUnit.Namespaces.Count - 1; i >= 0; i--)
                    {
                        if (visit.codeCompileUnit.Namespaces[i].Types.Count == 0)
                        {
                            visit.codeCompileUnit.Namespaces.RemoveAt(i);
                        }
                    }

                    TemplateCompileUnit = visit.codeCompileUnit;
                }
            }
        }
 public MonoRailGenerator(CodeGenerationContext context)
 {
     Context = context;
 }
Exemple #3
0
 public MonoRailGenerator(CodeGenerationContext context)
 {
     Context = context;
 }
 public CodeGenerationHelper(CodeGenerationContext context)
 {
     Context = context;
 }
        /// <summary>
        /// If automatic associations are enabled, this code generates a class that will be used to access a special internal property.
        /// </summary>
        /// <param name="model"></param>
        /// <param name="nameSpace"></param>
        private void GenerateInternalPropertyAccessor(CodeGenerationContext context, Model model, CodeNamespace nameSpace)
        {
            if (model.AutomaticAssociations)
            {
                /* Generates a class that looks something like this:
                 *     public class InternalPropertyAccessor : IPropertyAccessor
                 *     {
                 *         BasicPropertyAccessor accessor = new BasicPropertyAccessor();
                 *         
                 *         public IGetter GetGetter(Type theClass, string propertyName)
                 *         {
                 *             return accessor.GetGetter(theClass, propertyName + "Internal");
                 *         }
                 *         
                 *         public ISetter GetSetter(Type theClass, string propertyName)
                 *         {
                 *             return accessor.GetSetter(theClass, propertyName + "Internal");
                 *         }
                 *         
                 *         public bool CanAccessThroughReflectionOptimizer
                 *         {
                 *             get { return false; }
                 *         }
                 */

                nameSpace.Imports.Add(new CodeNamespaceImport("NHibernate.Properties"));
                CodeTypeDeclaration accessorClass = new CodeTypeDeclaration(context.InternalPropertyAccessorName);
                nameSpace.Types.Add(accessorClass);
                accessorClass.BaseTypes.Add("IPropertyAccessor");

                CodeMemberField accessorField = new CodeMemberField("BasicPropertyAccessor", "accessor");
                accessorClass.Members.Add(accessorField);
                accessorField.InitExpression = new CodeObjectCreateExpression("BasicPropertyAccessor");

                accessorClass.Members.Add(GetterOrSetterMethod("Getter"));

                accessorClass.Members.Add(GetterOrSetterMethod("Setter"));

                CodeMemberProperty canAccessProperty = new CodeMemberProperty();
                accessorClass.Members.Add(canAccessProperty);
                canAccessProperty.Type = new CodeTypeReference("Boolean");
                canAccessProperty.Name = "CanAccessThroughReflectionOptimizer";
                canAccessProperty.Attributes = MemberAttributes.Public | MemberAttributes.Final;
                canAccessProperty.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(false)));
            }
        }