在运行时获取类的对象 [英] Get the object of class at run time

查看:94
本文介绍了在运行时获取类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

项目包含三个类,一个类用于Access SQlScript,第二类包含SQl服务器查询,第三类包含Oracle Query类,我必须根据用户选择的数据源创建上述类之一的对象,它可能是SQL Server或Access或Oracle.我不想采用抽象类或接口并覆盖上述类中的方法,这是任何其他方法.

我可以使用反射或其他任何方法吗?

请回复我
在此先感谢

Sudhir Srivastava

Hi All,

Project contains three classes one class for Access SQlScript and second class contains SQl server Query and third class contains Oracle Query class, I have to create the object of one of the class mentioned above according to the datasource selected by user it may be SQL Server or Access or Oracle. I don''t want to take the abstract class or interfaces and override the method in the above mentioned class, is any other way to do this .

Can I use reflection or anything else.

Plz reply me
Thanks in advance

Sudhir Srivastava

推荐答案

做到这一点的标准方法是使用称为Provider模式的东西.基本上,您的类将是数据访问提供程序,并且您将使用.NET中的内置提供程序支持来实际运行适当的方法. 此处 [
The standard way of doing this would be to use something called the Provider pattern. Basically, your classes would be data access providers and you would use the inbuilt provider support in .NET to actually run the appropriate methods. Here''s[^] a fair demonstration of how you can use it (and it''s not limited to web apps - you can use it in desktop apps as well).


您可以在运行时使用反射来创建对象-但是,反射需要大量的性能.
如果您可以使反射无效并使用接口进行后期绑定,则它可能会更快.
You can use reflection to create objects at runtime - however, reflection is performance intensive.
If you can void reflection and do late binding using interfaces, it might be faster.


您可以看看 ^ ].


这篇关于在运行时获取类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆