.net教程:用system.web.caching.cache保存数据类的缓存
[size=3] public class dataprovider{
public static idataprovider instance()
{
//use the cache because the reflection used later is expensive
cache cache = system.web.httpcontext.current.cache;
if ( cache["idataprovider"] == null )
{
//get the assembly path and class name from web.config
string prefix = "";
namevaluecollection context =
(namevaluecollection)configurationsettings.getconfig("appsettings");
if (context == null)
{
//can not get settings
return null;
}
string assemblypath = context[prefix +
"dataproviderassemblypath"];
string classname = context[prefix +
"dataproviderclassname"];
// assemblypath presented in virtual form, must convert to
physical path
assemblypath =
httpcontext.current.server.mappath(httpcontext.current.request.applicationpath + "/bin/" +
assemblypath);
// uuse reflection to store the constructor of the class
that implements iwebforumdataprovider
try
{
cache.insert( "idataprovider", assembly.loadfrom(
assemblypath).gettype( classname ).getconstructor(new type[0]), new cachedependency(
assemblypath ) );
}
catch (exception)
{
// could not locate dll file
httpcontext.current.response.write("<b>error:</b>
could not locate file: <code>" + assemblypath + "</code> or could not locate class <code>" +
classname + "</code> in file.");
httpcontext.current.response.end();
}
}
return (idataprovider)(
((constructorinfo)cache["idataprovider"]).invoke(null) );
}
}
[/size]
页:
[1]