Search code examples
c#reflectionconstructor

How to write a program in C# to go through the parameters of a constructor in each class of the folder?


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.


Solution

  • 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