In the past two days, we have re-built the data center charging system. Reflection + configuration files are used to absorb database access programs.
During this period, I encountered a small problem, but it took a lot of time to complete it. The final problem is indeed a small detail.
See the specific code below
/Remove the ifactory, sqlserverfactory, sqlserverfactory, and accessfactory factory classes. replace them with the dataaccess class dataaccess {// reflection condition writing method. You can use case strings to instantiate objects by adding reflection. The variable is Private Static readonly string assemblyname = "reflection"; // The Assembly name is Private Static readonly string DB = configurationmanager. deleetask[ "DB"]; // database access variable // instantiate a specific class public static iuser createuser () {string classname = assemblyname + ". "+ dB +" user "; // namespace + instantiate class name return (iuser) assembly. load (assemblyname ). createinstance (classname);} public static idepartment createdepartment () {string classname = assemblyname + ". "+ dB +" department "; Return (idepartment) assembly. load (assemblyname ). createinstance (classname );}}
Configuration File Code
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup><appSettings><add key="DB " value="Sqlserver"/></appSettings></configuration>
Then the program is debugged. An error encountered during this period is
Then let's look at the value of classname, as shown in
Where is the database value missing? Go back to the database.
If the DB value is null, It is not passed. Check the configuration file and find that there is a space behind the DB in key = "DB ".
<appSettings><add key="DB " value="Sqlserver"/>
The problem is solved after a long struggle.The Details determine success or failure. A space is so troublesome. If you are not careful, the problem will be even greater. So be careful.