private void AddEnumerator(ClassDeclaration c, FieldDeclaration data, MethodDeclaration close) { c.Interfaces.Add(typeof(IEnumerable)); // create subclass ClassDeclaration en = c.AddClass("Enumerator"); // add wrapped field FieldDeclaration wrapped = en.AddField( c,"wrapped" ); ITypeDeclaration enumeratorType = new TypeTypeDeclaration(typeof(IEnumerator)); ITypeDeclaration disposableType = new TypeTypeDeclaration(typeof(IDisposable)); // add IEnumerator en.Interfaces.Add(enumeratorType); en.Interfaces.Add(disposableType); // add constructor ConstructorDeclaration cs = en.AddConstructor(); ParameterDeclaration collection = cs.Signature.Parameters.Add(c,"collection",true); cs.Body.AddAssign(Expr.This.Field(wrapped),Expr.Arg(collection)); // add current PropertyDeclaration current = en.AddProperty(data.Type,"Current"); current.Get.Return( Expr.This.Field(wrapped).Prop("Data") ); // add explicit interface implementation PropertyDeclaration currentEn = en.AddProperty(typeof(Object),"Current"); currentEn.Get.Return(Expr.This.Prop(current)); currentEn.PrivateImplementationType = enumeratorType; // add reset MethodDeclaration reset = en.AddMethod("Reset"); reset.ImplementationTypes.Add( wrapped.Type ); reset.Body.Add( Stm.Throw(typeof(InvalidOperationException),Expr.Prim("Not supported"))); // add movenext MethodDeclaration movenext = en.AddMethod("MoveNext"); movenext.ImplementationTypes.Add(wrapped.Type ); movenext.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool)); movenext.Body.Return( Expr.This.Field(wrapped).Method("Read").Invoke()); // add dispose MethodDeclaration disposeEn = en.AddMethod("Dispose"); disposeEn.ImplementationTypes.Add( disposableType ); disposeEn.Body.Add( Expr.This.Field(wrapped).Method(close).Invoke() ); disposeEn.Body.AddAssign( Expr.This.Field(wrapped),Expr.Null ); // add get enuemrator MethodDeclaration geten = c.AddMethod("GetEnumerator"); geten.Signature.ReturnType = en; geten.Body.Return(Expr.New(en,Expr.This)); MethodDeclaration igeten = c.AddMethod("GetEnumerator"); igeten.PrivateImplementationType = new TypeTypeDeclaration(typeof(IEnumerable)); igeten.Signature.ReturnType = new TypeTypeDeclaration(typeof(IEnumerator)); igeten.Body.Return(Expr.This.Method("GetEnumerator").Invoke()); }
private void AddCollectionMethods(ClassDeclaration col, ITypeDeclaration mappedType, string name, string pname) { // add method MethodDeclaration add = col.AddMethod("Add"+name); ParameterDeclaration para = add.Signature.Parameters.Add(mappedType,pname,true); add.Body.Add( Expr.This.Prop("List").Method("Add").Invoke(para) ); // add method MethodDeclaration contains = col.AddMethod("Contains"+name); contains.Signature.ReturnType = new TypeTypeDeclaration(typeof(bool)); para = contains.Signature.Parameters.Add(mappedType,pname,true); contains.Body.Return( Expr.This.Prop("List").Method("Contains").Invoke(para) ); // add method MethodDeclaration remove = col.AddMethod("Remove"+name); para = remove.Signature.Parameters.Add(mappedType,pname,true); remove.Body.Add( Expr.This.Prop("List").Method("Remove").Invoke(para) ); }