[C# 2.0] Comment charger une assembly dynamiquement ?
Bonjour,
voilà mon problème. Mon objectif est de pouvoir charger une Assembly dont le nom est dans un fichier de config : App.config.
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="data" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</configSections>
<data>
<add key="AssemblyLongFormName" value="Pape02.DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
<add key="ClassName" value="Pape02.DataAccess.Factory" />
<add key="Singleton" value="Instance" />
</data>
</configuration> |
Ci-dessous mon code pour charger mon Assembly :
Code:
1 2 3 4 5 6 7 8 9 10
| //récupère les informations du fichier de configuration
NameValueCollection dico = (NameValueCollection)ConfigurationManager.GetSection("data");
//charge l'assembly
Assembly assemb = Assembly.Load(dico["AssemblyLongFormName"].ToString());
//récupère le type
Type type = assemb.GetType(dico["ClassName"].ToString());
//récupère la propriétés
PropertyInfo prop = type.GetProperty(dico["Singleton"].ToString());
//retourne le singleton
return (IDataAccessFactory)prop.GetGetMethod().Invoke(null, null); |
Je précise que l'erreur se situe à la commande Assembly.Load.
L'erreur est "La référence d'objet n'est pas définie à une instance d'un objet"
Merci d'avance pour votre aide.
Damien