Пример #1
0
        public void Generate(System.Extensions.CodeDom.NamespaceDeclaration ns)
        {
            if (this.Name == null)
            {
                throw new InvalidOperationException("name not set");
            }

            // create class
            ClassDeclaration c = ns.AddClass(DecorateName(this.Name));

            this.Properties = new Hashtable();
            // add fields and properties
            foreach (DictionaryEntry de in this.Fields)
            {
                FieldDeclaration f = c.AddField(de.Value.ToString(), de.Key.ToString());

                PropertyDeclaration p = c.AddProperty(f, true, !ReadOnly, false);
                this.Properties.Add(de, p);
            }
        }
Пример #2
0
        public void Generate(System.Extensions.CodeDom.NamespaceDeclaration ns)
        {
            if (this.Name == null)
            {
                throw new InvalidOperationException("name not set");
            }

            // generate data
            this.Data.Generate(ns);

            // generate the rest
            ns.AddImport("System.Data");

            // create class
            ClassDeclaration c = ns.AddClass(this.Name + "DataReader");

            // IDisposable
            c.Interfaces.Add(typeof(IDisposable));

            // add datareader field
            FieldDeclaration dr = c.AddField(typeof(IDataReader), "dr");

            // add data field
            FieldDeclaration data = c.AddField(
                DataTemplate.DecorateName(this.Data.Name)
                , "data");

            data.InitExpression =
                Expr.New(data.Type);
            PropertyDeclaration datap = c.AddProperty(data, true, false, false);

            // foreach field values, add get property
            foreach (DictionaryEntry de in this.Data.Properties)
            {
                DictionaryEntry     dde = (DictionaryEntry)de.Key;
                PropertyDeclaration pd  = (PropertyDeclaration)de.Value;

                PropertyDeclaration pcd = c.AddProperty(pd.Type, pd.Name);
                pcd.Get.Return(
                    Expr.This.Field(data).Prop(pd)
                    );
            }


            // add constructor
            ConstructorDeclaration cs  = c.AddConstructor();
            ParameterDeclaration   drp = cs.Signature.AddParam(dr.Type, "dr", false);

            cs.Body.Add(Stm.ThrowIfNull(drp));
            cs.Body.Add(
                Stm.Assign(
                    Expr.This.Field(dr),
                    Expr.Arg(drp)
                    )
                );

            // add close method
            MethodDeclaration close = c.AddMethod("Close");

            // if dr ==null return;
            close.Body.Add(
                Stm.IfNull(Expr.This.Field(dr), Stm.Return())
                );
            // dr.Close();
            close.Body.Add(
                Expr.This.Field(dr).Method("Close").Invoke()
                );
            // dr = null;
            close.Body.AddAssign(Expr.This.Field(dr), Expr.Null);
            // data = null
            close.Body.AddAssign(Expr.This.Field(data), Expr.Null);

            // add read method
            MethodDeclaration read = c.AddMethod("Read");

            read.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool));

            // if (!dr.Read()){close and return)
            ConditionStatement ifnotread = Stm.IfIdentity(
                Expr.This.Field(dr).Method("Read").Invoke(),
                Expr.False,
                Stm.ToStm(Expr.This.Method(close).Invoke()),
                Stm.Return(Expr.False)
                );

            read.Body.Add(ifnotread);


            // foreach field values
            foreach (DictionaryEntry de in this.Data.Properties)
            {
                DictionaryEntry     dde = (DictionaryEntry)de.Key;
                PropertyDeclaration pd  = (PropertyDeclaration)de.Value;
                read.Body.AddAssign(
                    Expr.This.Field(data).Prop(pd),
                    (
                        Expr.This.Field(dr).Item(Expr.Prim(dde.Key.ToString()))
                    ).Cast(dde.Value.ToString())
                    );
            }
            // return true
            read.Body.Return(Expr.True);

            // add dispose method
            MethodDeclaration dispose = c.AddMethod("Dispose");

            dispose.ImplementationTypes.Add(typeof(IDisposable));

            // Close();
            dispose.Body.Add(
                Expr.This.Method(close).Invoke()
                );

            if (this.Enumerator)
            {
                AddEnumerator(c, data, close);
            }
        }
 internal ClassDeclaration(string name, NamespaceDeclaration ns)
     : base(name, ns.Conformer)
 {
     this.ns         = ns;
     this.Attributes = MemberAttributes.Public;
 }