反射加载

从SmartCoder里提取出来的代码:反射加载代码片断

过滤器有很多个备选,动态选择。

private IFilter getFilter(string filterName)
{
Type o = Type.GetType(filterName);//加载类型
IFilter filter = (IFilter)Activator.CreateInstance(o, true);
return filter;
}

<?xml version=”1.0″ encoding=”utf-8″ ?>
<configuration>
<configSections>
<section name=”FilterSetting” type=”TemplateEngine.FilterSettingSection, TemplateEngine, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null” allowDefinition=”Everywhere” allowExeDefinition=”MachineToApplication” restartOnExternalChanges=”true” />
</configSections>
<FilterSetting default=”DefaultFilter”>
<Filters>
<clear />
<add name=”DefaultFilter” type=”TemplateEngine.Filter.DefaultFilter,TemplateEngine,Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”/>
<add name=”Filter1″ type=”TemplateEngine.DefaultFilter,Test”  />
<add name=”Filter2″ type=”Test.Filter2,Test”  />
<add name=”Filter3″ type=”Test.Filter3,Test”  />
<add name=”Filter4″ type=”Test.Filter4,Test”  />
</Filters>
</FilterSetting>
</configuration>

Leave a Reply