mardi 9 octobre 2018

Type.GetType("namespace.classname, assemblyname") returns null, in a solution with main project and two class library projects

So I have a visual studio solution with 3 projects in it:

  1. A class library BLL project that contains EmployeeBLL.cs
  2. A second class library DAL project that has MemoryProvider.cs
  3. A WPF project, that has EmployeeViewModel.cs and an app.config file

Code:

  • App.config - appsettings

    add key="EmployeeMemoryProvider" value="EmployeeMemoryProvider.MemoryProvider,EmployeeMemoryProvider"

  • EmployeeViewModel.cs - constructor:

    public EmployeeViewModel()
    {
        var configString = ConfigurationManager.AppSettings["EmployeeMemoryProvider"];
        _employeeBLL = new EmployeeBLL(configString);
        var employeeList = new ObservableCollection<Employee>(_employeeBLL.GetAllEmployess());
    }
    
    

    }

  • EmployeeBLL.cs, constructor:

    public EmployeeBLL(string configString)
    {
        var providerType = Type.GetType(configString);
        _employeeProvider = (IEmployeeProvider)Activator.CreateInstance(providerType);
    }
    
    

Goal: I want to get the type of the string from the App.config, which the string is first obtained in EmployeeViewModel.cs as configString then passed into the constructor of EmployeeBLL.cs and finally use the Type.GetType(configString) to get the type of configString

Problem: the providerType returns null, even though the assembly name and namespace of the DAL project are both EmployeeMemoryProvider.

Can anyone shed some light? Is it because I missed a space after the comma in "EmployeeMemoryProvider.MemoryProvider,EmployeeMemoryProvider"?





Aucun commentaire:

Enregistrer un commentaire