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()); }
public MethodDeclaration AddMethod(string name) { if (name==null) throw new ArgumentNullException("name"); MethodDeclaration m = new MethodDeclaration(name,this); this.methods.Add(m); return m; }
/// <summary> /// Removes the first occurrence of a specific MethodDeclaration from this MethodDeclarationCollection. /// </summary> /// <param name="value"> /// The MethodDeclaration value to remove from this MethodDeclarationCollection. /// </param> public virtual void Remove(MethodDeclaration value) { this.List.Remove(value); }
/// <summary> /// Initializes a new instance of the MethodDeclarationCollection class, containing elements /// copied from an array. /// </summary> /// <param name="items"> /// The array whose elements are to be added to the new MethodDeclarationCollection. /// </param> public MethodDeclarationCollection(MethodDeclaration[] items) { this.AddRange(items); }
/// <summary> /// Inserts an element into the MethodDeclarationCollection at the specified index /// </summary> /// <param name="index"> /// The index at which the MethodDeclaration is to be inserted. /// </param> /// <param name="value"> /// The MethodDeclaration to insert. /// </param> public virtual void Insert(int index, MethodDeclaration value) { this.List.Insert(index, value); }
/// <summary> /// Return the zero-based index of the first occurrence of a specific value /// in this MethodDeclarationCollection /// </summary> /// <param name="value"> /// The MethodDeclaration value to locate in the MethodDeclarationCollection. /// </param> /// <returns> /// The zero-based index of the first occurrence of the _ELEMENT value if found; /// -1 otherwise. /// </returns> public virtual int IndexOf(MethodDeclaration value) { return this.List.IndexOf(value); }
/// <summary> /// Determines whether a specfic MethodDeclaration value is in this MethodDeclarationCollection. /// </summary> /// <param name="value"> /// The MethodDeclaration value to locate in this MethodDeclarationCollection. /// </param> /// <returns> /// true if value is found in this MethodDeclarationCollection; /// false otherwise. /// </returns> public virtual bool Contains(MethodDeclaration value) { return this.List.Contains(value); }
/// <summary> /// Adds the elements of an array to the end of this MethodDeclarationCollection. /// </summary> /// <param name="items"> /// The array whose elements are to be added to the end of this MethodDeclarationCollection. /// </param> public virtual void AddRange(MethodDeclaration[] items) { foreach (MethodDeclaration item in items) { this.List.Add(item); } }
/// <summary> /// Adds an instance of type MethodDeclaration to the end of this MethodDeclarationCollection. /// </summary> /// <param name="value"> /// The MethodDeclaration to be added to the end of this MethodDeclarationCollection. /// </param> public virtual void Add(MethodDeclaration value) { this.List.Add(value); }