Problems with the reflection mechanism of C #

Source: Internet
Author: User
The problem project Dalfactory is the factory pattern design, the design pattern book I have also seen the Java design pattern, the understanding is not very deep, but is more familiar with the factory pattern, uses it to return the different instance object according to the demand, Using the reflection mechanism to implement dependency injection in Dalfactory projects, of course, is not as flexible and powerful as spring in Java, and some of the code is as follows:

<summary>
Abstract factory pattern creates dal.
Web.config need to add configuration: (using Factory mode + reflection mechanism + caching mechanism, implement dynamic creation of different data-Layer object interface)
Datacache class in the folder where the code is exported
The creation of all DAL classes can be placed in this dataaccess class.
<appSettings>
<add key= "DAL" value= "Smssystem.sqlserverdal"/> (the namespace here is changed to the namespace of your own project according to the actual situation)
</appSettings>
</summary>
public sealed class DataAccess
{
private static readonly string path = configurationmanager.appsettings["DAL"];
<summary>
Create an object or get it from the cache
</summary>
public static object CreateObject (string path, String cachekey)
{
Object objType = Datacache.getcache (CacheKey);//read from cache
if (ObjType = null)
{
Try
{
Assembly ass = new Assembly ();
ObjType = Assembly.Load (path). CreateInstance (CacheKey);//Reflection Creation
Datacache.setcache (CacheKey, objType);/write Cache
}
catch (System.Exception ex)
{
String str = ex. message;//
SmsSystem.Utility.SaveLog.SaveInfoToLog (str, "ErrorLog", "anomaly");
}
}
return objType;
}
<summary>
Create an object without using the cache
</summary>
Private static Object Createobjectnocache (string path, String cachekey)
{
Try
{
Object objType = Assembly.Load (path). CreateInstance (CacheKey);
return objType;
}
catch//(System.Exception ex)
{
String Str=ex. message;//Logging error Log
return null;
}
}
<summary>
Creating Custemployee data-tier interfaces
</summary>
public static SmsSystem.IDAL.ICustEmployee Createcustemployee ()
{
String CacheKey = Path + ". Custemployee ";
Object ObjType = CreateObject (path, CacheKey);
Return (Icustemployee) ObjType;
}
.................. (Other data layer interface)
}
By this class, you can get an instance of the data access layer that you want to use, but I always throw an exception while I'm using it, [System.IO.FileNotFoundException] = {"Failed to load file or assembly" Smssystem.sqlserverdal "or one of its dependencies. The system could not find the specified file. ":" "Smssystem.sqlserverdal"}, to tell the truth, for C # This row wrong and debugging I hit back, I baffled, why would a reflection on the anomaly, the whole up to see the reflection of the IPA, I feel my usage is not wrong ah, moreover, I see petshop example is such a similar way of writing, why it can be used, the afternoon debugging a long time or in error, I sent hard-hearted to and petshop to contrast, and then finally found that it's the name of the DLL some strange, and my differences, It's all like PetShop.SQLServerDAL.dll name, and mine is a SQLServerDAL.dll, I feel very likely there is a problem, because in Wb.config I have configured

<appSettings>
<add key= "DAL" value= "Smssystem.sqlserverdal"/>
............
</appSettings>
Such an assembly, for a long time, consult others, the results are clear, is to project properties to set the name of the assembly, you can generate SmsSystem.SQLServerDAL.dll DLL file


Setup, regenerate, and a little problem,

The type "SmsSystem.BLL.CustEmployee" also exists in "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary asp.net Files"
\smssystem\3358d7f2\16ba9bb7\assembly\dl3\7296d5df\40ded961_96d8c601\bll. DLL "
and "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary asp.net files\smssystem\3358d7f2
\16ba9bb7\assembly\dl3\644d894c\4cf8840a_9ad8c601\smssystem.bll.dll "
Zhong G:\ASP.NET\SmsSystem\Default.aspx.cs 22

I looked carefully, originally in the Web Project bin under, The original SQLServerDAL.dll still in, and a SmsSystem.SQLServerDAL.dll, haha, vs2005 another not automatically deleted redundant files, yesterday is a project file AssemblyInfo.cs deleted, in the corresponding delete file unexpectedly did not delete, However, I remove it manually, OK, all normal, or thank you tell me how to change the project assembly name friends, or I unexpectedly find a long time also do not know how to generate a namespace with the DLL.

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.