怎么能让C#程序可扩充 用户给定一种文件,我们要通过一个类来读取,但是读取的结果形式都是已知的,可以被程序使用。我们像把读取功能单独做成模块,如果将来建立新的文件形式,我们只需要增加一个模块就可以了。不知道这种目的在C#里怎样实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 使用类厂:public interface IReader{ DataResult Read();}public class ClassFactory{ IReader QueryInterface(string FileType) { switch(FileType) { "excel": return new ExcelReader(); "xml": return new XMLReader(); .... } }}public class ExcelReader: IReader{ public DataResult Read() { //读取excel文件的数据 }}public class XMLReader: IReader{ public DataResult Read() { //读取XML文件的数据 }} 真的非常感谢ExcelReader、 XMLReader可以做成类库的形式但是 IReader QueryInterface(string FileType) { switch(FileType) { "excel": return new ExcelReader(); "xml": return new XMLReader(); .... } }还是在程序里定义好的。可不可以像插件那样扩展? 是的用反射加载你还需要维护一个 文件类型和每个类库之间的对应关系类似这样:文件类型 类库"excel" "ExcelReader""xml" "XMLReader""txt" "TxtReader".....然后在类厂中根据文件类型(参数), 使用反射加载对应的类库,创建IReader [code=C# public class ClassFactory { //ArrayList m_Plugin; static public IDPlugin.IPlugin GetInterface(string FileName,string Plugin) { Assembly dll; dll = System.Reflection.Assembly.LoadFile(FileName); Type t; Plugin = "IDPlugin."+Plugin; t = dll.GetType(Plugin); if(t==null) throw new Exception( "There is not "+Plugin); if(t.GetInterface("IDPlugin.IPlugin")!=null) return (IDPlugin.IPlugin)Activator.CreateInstance(t); else return null; } }][/code] 考试时间倒计时 SQL分页获得不了第一条记录 访问excel 谁能帮忙把这段VB代买改写成C#的? 断点调试正确,不设断点就出错 我想创建自定义的项目模板 和自定义类文件模板,那位高手做过给你思路!!! 请问Listbox的项有Tag属性吗? C#高手请进 请问:如何保证应用程序只有一个实例运行? c#调用帮助文件chm,谁弄过?(宁波可以请吃肯德鸡) 在线等.控件问题. 浮出水面吧,高手们..我要把两个GridView之间绑定
public interface IReader
{
DataResult Read();
}
public class ClassFactory
{
IReader QueryInterface(string FileType)
{
switch(FileType)
{
"excel":
return new ExcelReader();
"xml":
return new XMLReader();
....
}
}
}public class ExcelReader: IReader
{
public DataResult Read()
{
//读取excel文件的数据
}
}public class XMLReader: IReader
{
public DataResult Read()
{
//读取XML文件的数据
}
}
ExcelReader、 XMLReader可以做成类库的形式但是 IReader QueryInterface(string FileType)
{
switch(FileType)
{
"excel":
return new ExcelReader();
"xml":
return new XMLReader();
....
}
}
还是在程序里定义好的。可不可以像插件那样扩展?
你还需要维护一个 文件类型和每个类库之间的对应关系
类似这样:文件类型 类库
"excel" "ExcelReader"
"xml" "XMLReader"
"txt" "TxtReader"
.....然后在类厂中根据文件类型(参数), 使用反射加载对应的类库,创建IReader
{
//ArrayList m_Plugin; static public IDPlugin.IPlugin GetInterface(string FileName,string Plugin)
{
Assembly dll;
dll = System.Reflection.Assembly.LoadFile(FileName);
Type t;
Plugin = "IDPlugin."+Plugin;
t = dll.GetType(Plugin);
if(t==null) throw new Exception( "There is not "+Plugin); if(t.GetInterface("IDPlugin.IPlugin")!=null) return (IDPlugin.IPlugin)Activator.CreateInstance(t);
else
return null; } }][/code]