1

i wrote some code to get all classes implementing an Interface.

private static List<ClassNameController> getClassesByInheritInterface(Type interfaceName)
{

        var types = AppDomain.CurrentDomain.GetAssemblies()
            .SelectMany(s => s.GetTypes())
            .Where(p => interfaceName.IsAssignableFrom(p) && !p.IsInterface);
        List<ClassNameController> myControllerList = new List<ClassNameController>();
        foreach (System.Type type in types)
        {
            // Get a PropertyInfo of specific property type(T).GetProperty(....)
            PropertyInfo propertyInfo;
            propertyInfo = type
                .GetProperty("className", BindingFlags.Public | BindingFlags.Static);

            // Use the PropertyInfo to retrieve the value from the type by not passing in an instance
            object value = propertyInfo.GetValue(null, null);

            // Cast the value to the desired type
            string typedValue = (string)value;

            myControllerList.Add(new ClassNameController(typedValue, type));
        }

        return myControllerList;
    }
}

All of these classes got a public static string className Property. The Value of this Property I use to create an ClassNameController Instance

class ClassNameController
{
    public string Name { get; set; }
    public System.Type ObjectType { get; set; }

    public ClassNameController(string name, Type objectType)
    {
        this.Name = name;
        this.ObjectType = objectType;
    }

    public override string ToString()
    {
        return Name;
    }
}

But when i start my Program it crashes at

object value = propertyInfo.GetValue(null, null);

with the error message

System.NullReferenceException.

Question: So why cant he find the Property Classname?

Edit: All Classes are implementing these interfaces are WPF UserControls. For example IModuleview:

  internal interface IModuleView
{
    void updateShownInformation();

    void setLanguageSpecificStrings();
}

And here an example of a Module:

   public partial class DateBox : UserControl, IModuleView
{
    public static string className = "Datebox";
    public DateBox()
    {
        InitializeComponent();
    }

    public void setLanguageSpecificStrings()
    {
        this.ToolTip = DateTime.Now.ToString("dddd, dd.MM.yy");
    }

    public void updateShownInformation()
    {
        tbDate.Text = DateTime.Now.ToString("ddd-dd");
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
itskajo
  • 285
  • 2
  • 19
  • Please provide a [mcve] - a lot of the code you've provided isn't relevant (e.g. the code to find implementations of the interface), but you haven't provided enough for us to actually run and see the problem. (I'd also suggest demonstrating this in a console app - I very much doubt that WPF is relevant here.) – Jon Skeet Feb 23 '17 at 12:15
  • @JonSkeet the classes which should be provided by this codeexample arre UserControls from WPF – itskajo Feb 23 '17 at 12:18
  • 1
    Just because that's how they're used in your application doesn't mean they need to be `UserControl` classes to demonstrate the problem. When you ask a question on Stack Overflow, you should try to reduce it as far as possible - that's part of the diagnostic work you should do *before* asking a question, which will often mean you don't need to ask the question at all. – Jon Skeet Feb 23 '17 at 13:38
  • And now you've posted a fuller example, we can see that indeed, it's because you've got a field rather than a property, and that has *nothing* to do with WPF. (I'd suggest it would be better as an attribute, mind you.) – Jon Skeet Feb 23 '17 at 13:38

1 Answers1

2

So why cant he find the Property Classname?

Looking at the declaration in your posted class DateBox:

public static string className = "Datebox";

It has the signature of a field

Hence you should use the GetField method:

object value = type.GetField("className", 
             System.Reflection.BindingFlags.Public | 
             System.Reflection.BindingFlags.Static).GetValue(null);

Explanation: What is the difference between a Field and a Property in C#?

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76