jeudi 16 novembre 2017

Get a Public Static Property in a Non-Static Singleton Class from a Dynamically Loaded Assembly

I am using Visual Studio 2015 update 3 and .NET 4.6.2 on a Windows 7 machine.

Goal - I would like to have 100% dynamic binding with a specific set of dlls. To do this, I load the dlls as required from the given location. I am able to get the correct class type and the correct property I am trying to access through its 'PropertyInfo'. The issue arises when I attempt to get the value from the property.

How I am loading the assembly.

var assembly = Assembly.LoadFrom("DLL LOCATION");
var type = assembly.GetType("Namespace and Class Name");

How I am attempting to get the static property value.

private static ReturnedType GetStaticProperty<ReturnedType>(string name, BindingFlags flags, Type type) where ReturnedType : class
{
    try
    {
        var properties = type.GetProperties(flags);
        var propertyInfo = properties.FirstOrDefault(x => x.Name.Equals(name));
        var test = propertyInfo.GetValue(null);
        return propertyInfo.GetValue(null) as ReturnedType;
     }
     catch (Exception)
     {
         return null;
     }
 }

Results from the 'GetStaticProperty'. More specifically the 'test' variable issue.

enter image description here

enter image description here

The file does in fact exist in the given path.





Aucun commentaire:

Enregistrer un commentaire