-2

I have a folder of classes with different namespaces. I need to write a program to go through each class and its constructor to list its parameters.

I tried taking the classes as a text file and reading the file to match the word constructor and list down the contents after that. But instead, I'm required to not change it to a text file.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 4
    i would use reflection for that instead of text parsing – Daniel A. White Nov 25 '22 at 14:14
  • What are you trying to do? If you want to analyze source files you can't use Reflection. You can create a [code analyzer](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix) though that will receive the information you want directly from the compiler. That's how Visual Studio's analyzers and fixers work. – Panagiotis Kanavos Nov 25 '22 at 14:16
  • 1
    we need way more information about your question - in particular your current code and where it fails your expactations. – MakePeaceGreatAgain Nov 25 '22 at 14:16
  • 1
    "instead I'm required to not change it to a text file" You don't need to change it to a txt-file, you can read a cs-file as text-file. But if these classes are available in your program, use reflection instead and don't parse text since it makes the task much easier. – Tim Schmelter Nov 25 '22 at 14:18

1 Answers1

0

You can achieve this by getting all the file names of a given folder. Here I am assuming that the file name and class names are the same. Otherwise, there is no way to get the files based on folder name then the only option is to pass all the namespaces and you can get the info. you can use this link for that.

And when you get the file names without extension then you can use reflection to get the constructors for a given type using Type.GetConstructors method in a loop.

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.type.getconstructors?view=net-7.0

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197