Transferred from: Model-engineering implementation and expansion (Design Mode C)
Http://www.cnblogs.com/callwangxiang/
1,Traverse the current folder to list all recent3Full path of files created or modified by Tianji
Reference answer
Using System;
Using System. diagnostics;
Using System. LINQ;
Using System. IO;
Using Microsoft. visualstudio. testtools. unittesting;
Namespace Marvellousworks. practicalpattern. iterator. Tests. Exercise
{
[Testclass]
Public Class Iteratefilefixture
{
Const String Path = @" . " ;
[Testmethod]
Public Void Testlistmodifiedandnewcreatedfiles ()
{
Func < Datetime, Bool > Lessthanthreedayshandler = X => (Datetime. Now - X). Days <= 3 ;
VaR files =
New Directoryinfo (PATH). getfiles (). Where (x =>
Lessthanthreedayshandler (X. lastwritetime) |
lessthanthreedayshandler (X. creationtime)
);
//There will be some newly compiled files in the current unit test directory.
Assert. istrue (files. Count ()> 0);
// Output matching information
Foreach (Var file In Files)
{
Trace. Write (file. fullname );
If (Lessthanthreedayshandler (file. lastwritetime ))
Trace. Write ( " \ TM " );
If (Lessthanthreedayshandler (file. creationtime ))
Trace. Write ( " \ TC " );
Trace. writeline ( "" );
}
}
}
}
------ Test started: Assembly: iterator . Tests . DLL ------
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . DLL C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . PDB C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . Tests . DLL M C
E: \ Marvelousworks \ Unittest \ Iterator . Tests \ Bin \ Debug \ Iterator . Tests . PDB M C
1Passed, 0Failed, 0Skipped,Took0.48Seconds(Mstest10.0).
2,UseAttributeTo ensure that the iterator can traverse related content based on different themes.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using Microsoft. visualstudio. testtools. unittesting;
Namespace Marvellousworks. practicalpattern. iterator. Tests. Exercise
{
[Testclass]
Public Class Attributsubjectediteratorfixture
{
[Serializable]
[Attributeusage (attributetargets. Class, allowmultiple = True )]
Class Iterableattribute: attribute {}
ClassSubjectaattribute: iterableattribute {}
ClassSubjectbattribute: iterableattribute {}
Static Class Subjectediterator
{
/// <Summary>
/// Traverse by specified topic-that is, attribute
/// </Summary>
/// <Typeparam name = "telement"> Set element type </Typeparam>
/// <Typeparam name = "tattribute"> Traverse the attribute type </Typeparam>
/// <Param name = "data"> </param>
/// <Returns> </returns>
Public Static Ienumerable < Telement > Getenumerator < Telement, tattribute > (Ienumerable < Telement > Data)
Where Tattribute: iterableattribute
{
If (Data = Null ) Return Null ;
If (Data. Count () = 0 ) Return Data;
Return Data. Where (x =>
X. GetType (). getcustomattributes ( Typeof (Tattribute ), False ). Length > 0 );
}
}
[Subjecta]
ClassA {}
[Subjectb]
ClassB {}
[Subjecta]
[Subjectb]
ClassAB {}
ClassO {}
[Testmethod]
Public Void Testiteratebysubject ()
{
VaR data = New List < Object > { New A (), New B (), New AB (), New A (), New O {}, New O {}, New AB ()};
VaR subjecta = Subjectediterator. getenumerator < Object , Subjectaattribute > (Data );
Assert. areequal < Int > ( 4 , Subjecta. Count ()); // Two A and two AB
VaR subjectb = Subjectediterator. getenumerator < Object , Subjectbattribute > (Data );
Assert. areequal < Int > ( 3 , Subjectb. Count ()); // One B and two AB
}
}
}