Configuration File problems in reflection

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.