Link to home
Start Free TrialLog in
Avatar of silentthread2k
silentthread2kFlag for United States of America

asked on

C#, Generic, Reuse of file enumeration

I'm trying to use my FileWorks class below to tell me if a directory has a certain file extension.
I'm trying to be as generic as possible and reuse the function...
GetFilesDoSomething

I'm not sure how to approach this one. This is the code I have below.

Does not look like it would work if I were to call it...

FileWorks filework = new FileWorks();
filework.GetFilesDoSomething(directory, CheckDirectoryForFilesWithExtension);

How can I pass the bool result back accross soo many functions?


public class FileWorks
    {
 
        bool CheckDirectoryForFilesWithExtension(FileInfo fi)
        {
            if (fi.Extension == "tif")
            {
                return true;
            }
        }
 
 
        public void GetFilesDoSomething(string DirName, Action<FileInfo> method)
        {
            DirectoryInfo d = new DirectoryInfo(DirName);
            FileInfo[] f = d.GetFiles();
 
            foreach (FileInfo fi in f)
            {
                method.Invoke(fi);
            }
        }
     }

Open in new window

Avatar of ToddBeaulieu
ToddBeaulieu
Flag of United States of America image

This looks like delegates 101, not generics.

Check this out:

https://www.experts-exchange.com/questions/24247143/Simple-calculator-using-delegates.html


ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial