0

I have edited my previous question because of lack of information.

I have written a class named ReaderFactory where I create a Method GetAReader(string filename).This method returns a reader using its file-name extension. I used switch-case to select the specific reader.But my instructor suggested me to use reflection instead of switch-case.I have started to learn reflection and got stuck on its behavior. How can I implement reflection instead of switch-case in my code? GetAReader method is given below :

 
        public static IReader GetAReader(string _fileName)
        {
            fileName = _fileName;
            fileExtension = Path.GetExtension(fileName);

        switch (fileExtension) 
        {
            case ".txt":
                     TextFileReader _textReader = new TextFileReader(fileName);
                         _textReader.ReadData();
                      return _textReader;

            case ".pdf":
                         PDFReader _pdfReader = new PDFReader(fileName);
                         _pdfReader.ReadData();
                         return _pdfReader;
            case ".doc":
                        //reader initialize
            case ".rtf":
                         //reader initialize
            case ".docx":
                        //reader initialize
            case ".jpg":
                        //reader initialize


        }

        return null;
    }
fyasir
  • 2,924
  • 2
  • 23
  • 36

1 Answers1

4

Reflection is something very complicated, but can be used for a wide variety of things. Some examples:

  • Reading properties by the specifying the name.
  • Load an assembly and get all the types of it.
  • Read attributes

To make it very short, with reflection you have the power to decompile any .NET code to C# / VB or any other IL language.

I suggest you have a look at telerik's decompiler (that's a tool that let you view source code of dll's). Than you have an idea what reflection is.

Alo, here's some documentation to get you started: http://msdn.microsoft.com/en-us/library/f7ykdhsy.aspx

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • I edited my question. can you please elaborate those question. – fyasir May 26 '14 at 16:23
  • Don't understand me wrong, but there are already a lot of articles on Reflection. Can you explain me why you need it? I ask this because reflection is quite expensive and if there's a built in .NET solution, that might be much quicker. I will give you the explanations you need, but I just want to avoid that u use code which requires unneeded resources. – Complexity May 26 '14 at 16:29
  • I have written a class named Reader.I used switch-case for selecting the file extension.As there are lots of file type my instructor suggested me to use reflection instead of switch-case.I started reading about reflection and got stuck on dynamically load assembly files.So can you please explain me How can I use reflection instead of switch-case? – fyasir May 26 '14 at 16:43
  • Ok, please edit your original question where you show your code and what the code is supposed to do. That way you will get better answers on your questions. – Complexity May 26 '14 at 16:44
  • Thanks for the suggestion.I am editing my original question – fyasir May 26 '14 at 17:00
  • Thanks. I will try to answer later today. – Complexity May 26 '14 at 17:04
  • I'm sorry but I don't know what your prof is saying. Can you tell me where he would use reflection and I what context? Since reflection is very wide it's important that we know the context. Reading attributes, get types, ... Anyway. I will post some examples still today about reflection to get you started. – Complexity May 26 '14 at 18:05
  • My instructor told me to use reflection instead of switch-case portion. – fyasir May 26 '14 at 18:24
  • I don't see how. I'm sorry. – Complexity May 26 '14 at 18:25
  • 1
    But I've looked around on the net and come accross to this post on StackOverflow. http://stackoverflow.com/questions/4234421/method-factory-case-vs-reflection It's a discussion to see what's faster a switch or reflection. A switch will always be faster, but maybe it's a good idea to read that post. And don't hesitate to come back if you have any further questions. – Complexity May 26 '14 at 18:29