-1

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
    }
}   
DannyP
  • 25
  • 6
  • 1
    `typeof(nameOfClass)` will just give you `string`. Use [`Type.GetType`](https://msdn.microsoft.com/en-us/library/w3f99sx1(v=vs.110).aspx) instead. – Ian H. Mar 08 '18 at 20:00
  • Why not use `typeof` where needed like you did on that line? Why would you pass a type by string? Expand your question and show how you intend to use this in your actual code. – Igor Mar 08 '18 at 20:00
  • Does `Thrift` generate `partial` classes? – mjwills Mar 08 '18 at 20:38
  • @mjwills it does – DannyP Mar 08 '18 at 20:57
  • Would it be possible, for example, to build this code in an abstract class, and then have all of your generated classes (via extra partial class files) inherit from this file? Allowing each class to have its own internal counter? – mjwills Mar 08 '18 at 20:58
  • I may have a solution... If I can get it working, I'll post an answer. – DannyP Mar 09 '18 at 20:46
  • I look forward to it @DannyP . – mjwills Mar 11 '18 at 11:53

2 Answers2

1

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;
            }
        }
    }
}
DannyP
  • 25
  • 6
-1

You could use Activator.CreateInstance, with the overload which accepts two strings: one for the assembly in which the type is located, and one which specifies the type (in your case, class).

https://msdn.microsoft.com/en-us/library/d133hta4(v=vs.110).aspx

public int PropertyCounter(string nameOfClass) {
    return Activator.CreateInstance(nameOfAssembly, 
      nameOfClass).GetType().GetProperties().Count();
}

You should check for failure

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129