Castro,

The collection and iterator classes aren't really necessary as you could use an array based property too :

[BuildElementCollection("filters", "pattern")]
public Pattern[] Patterns {
   get { return _patterns; }
   set { _patterns = value; }
}

but from a design perspective collections are preferred over arrays.

Gert

----- Original Message ----- From: "Castro, Edwin Gabriel (Firing Systems Engr.)" <[EMAIL PROTECTED]>
To: "Castro, Edwin Gabriel (Firing Systems Engr.)" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Tuesday, September 21, 2004 2:18 AM
Subject: RE: [Nant-users] proper format for a collection of strings in custom task



It turns out that I forgot to do a lot! I first needed to create a class derived from Element:



[ElementName("pattern")]

public class Pattern : Element {

   // ...

}



Then I needed to create a specialized Collection:



[Serializable()]

public class PatternCollection : CollectionBase {

   // ...

}



Finally I needed to create an IEnumerator:



public class PatternEnumerator : IEnumerator {

   // ...

}



I can now use Pattern in my task as follows:



[BuildElementCollection("filters", "pattern")]

public PatternCollection Patterns {

   get { return _patterns; }

}



Wow, that was lot of work! I used Option, OptionCollection,
OptionEnumerator, and ExecTask as examples.



Lots of work just to get my task to have the correct elements in it but
it worked automatically once I did it the right way!



Thanks to the NAnt team for the examples!



--Edwin



________________________________

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Castro,
Edwin Gabriel (Firing Systems Engr.)
Sent: Monday, September 20, 2004 3:20 PM
To: [EMAIL PROTECTED]
Subject: [Nant-users] proper format for a collection of strings in
custom task



Hi All,



I am writing my own custom task and I would like to have a collection of
strings as an element... something like the following:



<customtask>

   <filters>

       <pattern name="pattern1" />

       <pattern name="pattern2" />

       <pattern name="pattern3" />

   </filters>

</customtask>



My code looks like this:



[BuildElementCollection("filters", "pattern")]

public StringCollection Patterns {

   get { return _patterns; }

}



This didn't work.



I would like to be able to use the Patterns property in a foreach loop
something like this:



public void ProcessPatterns() {

   foreach (string pattern in Patterns) {

       // do something with pattern

   }

}



Could someone help me to get the syntax correct? Thanks!



--Edwin






------------------------------------------------------- This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 Project Admins to receive an Apple iPod Mini FREE for your judgement on who ports your project to Linux PPC the best. Sponsored by IBM. Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php _______________________________________________ Nant-users mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/nant-users

Reply via email to