I'm trying to create a method that counts the properties of a given class. I want to pass in the class name as a string perhaps and then can turn the string into a reference of the given class. I have literally hundreds of classes (generated by Thrift) that could be passed in, so its not practical to give each class its own property counter.
My purpose is to provide arguments to a class that dynamically creates a UI based on what will need to be input by the user for each specific method and what will be returned. To save myself from having to manually write a UI for every method.
Is there a good way to do this?
Here's what I have so far.
class PropertyCounter
{
public int PropertyCounter(string nameOfClass)
{
int count = typeof(nameOfClass).GetProperties().Count();
return count
}
}
I got this working... using Assembly. Took some doing but it does what i need it to do.
Now, I was thinking of making these into a list of 'class' objects, but I'm thinking a string would work just as well for an argument.
Thanks to all who offered assistance.
class Discover
{
public void DiscoverProperties()
{
var me = Assembly.GetExecutingAssembly().Location;
var dir = Path.GetDirectoryName(me);
var theClasses = dir + @"dllName.dll";
var assembly = Assembly.LoadFrom(theClasses);
var types = assembly.ExportedTypes.ToList();
int propCount;
string propertiesList;
string cName;
string tempString;
foreach (var t in types)
{
propertiesList = "";
propCount = 0;
cName = t.Name;
foreach (var prop in t.GetProperties())
{
propCount++;
tempString = $"{prop.Name}:{prop.PropertyType.Name} ";
propertiesList = propertiesList += tempString;
}
}
}
}