如何在Windows应用程序中调用新窗口 [英] how to invoke new window in windows applcation

查看:63
本文介绍了如何在Windows应用程序中调用新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

感谢您的回复
我会详细解释我的需求

通常我通过创建对象来调用Windows窗体.
如果我想要一个名为frmcompany的屏幕.我将代码写为

Hi all

Thanks for the response
i will explain my need in detail

usually i call windows form by creating object.
If i want a screen named frmcompany . i write code as

frmcompany obj=new frmcompany (); 
obj.show(); 



但是在我目前的情况下,此表单名称将在一个字符串变量中.将其命名为ls_com_name



But in my current case this form name will be in one string variable .let it be ls_com_name

ls_com_name="frmcompany ";//this value getting from database not constant 


如何从字符串ls_com_name调用frmcomapny.
没有像ls_com_name.show()这样的方法;
代替使用什么..


how to call frmcomapny from string ls_com_name.
there isn''t method like ls_com_name.show();
what to use instead..

推荐答案

让我们说您正在将字符串作为"ClassA"获得,并且想要创建其对象.

如果您可以使用动态类型变量,请执行此操作
Lets say you are getting the string as "ClassA" and you want to create its object.

if you can use dynamic type variables then do this
<br />
var d1 = Type.GetType("ClassA");<br />


如果没有,


if not then

<br />
object d1 = Type.GetType("ClassA");


您应该问自己,在进行基础技术研究之前,如何首先将名称放入数据库中.现在您正在询问调用……您应该先考虑,然后再做决定.我认为这不是一个好主意.

基本上,可以有一个类名,但是最好有一个完整的类名,以确保使用正确的名称空间和正确的程序集.也可以根据数据库中存储的签名来检查程序集签名.您会看到,您输入了一些不可靠的字段,其中错误的程序集(或错误的版本)可能包含具有相同名称和大小写的不同类型.

您可以使用反射按名称查找所需的类型.使用方法System.Reflection.Assembly.GetType(String)完成此操作.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ].

要创建实例(以您的情况为形式),需要调用类型构造函数.
您需要找到没有参数的构造函数,获取类System.Reflection.ConstructorInfo的实例(显然,只有一个具有此签名的构造函数),并使用其方法System.Reflection.ConstructorInfo.Invoke创建一个实例.
请参阅:
http://msdn.microsoft.com/en-us/library/system.type.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.reflection. constructorinfo.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx [ ^ ].

即使它在概念上看起来很复杂,但是如果您简单地阅读上面引用的MSDN文章中涉及的所有反射类型和方法,并按照我解释的顺序执行所有步骤,则实现起来非常容易.

但我要警告您:您的方法可能会立即生效,但是仍然需要进行审查.当您开始修改系统并提供支持时,您可能会陷入噩梦.软件版本和数据库中数据之间的各种不兼容性可能会严重困扰您.您需要非常全面地计划所有方案.首先,修改数据库中字符串类名称(或其他一些与代码相关的实体)的整体概念.这是潜在的认真架构,需要认真的架构师...

祝你好运,
—SA
You should ask yourself how come you first put the names in the database before doing elementary technological research. Now you are asking about invocation… You should think first, make decision later. I don''t think this is a good idea.

Basically, you can have a class name, but you should better have a full class name, to make sure you are working with right namespace and right assembly. It''s also good to check up assembly signature against the one stored in the database. You see, you have entered in some non-reliable field where the wrong assembly (or a wrong versions) can contain different types with the same names and cases like that.

You can find the type you need by its name using Reflection. This is done using the method System.Reflection.Assembly.GetType(String). Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^].

The invocation of the type constructor is required to create an instance (of the form, in your case).
You need to find constructor without parameters, get an instance of the class System.Reflection.ConstructorInfo (apparently, there can be only one constructor with this signature) and create an instance using its method System.Reflection.ConstructorInfo.Invoke.

Please see:
http://msdn.microsoft.com/en-us/library/system.type.aspx[^],
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.aspx[^],
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx[^].

Even though it looks conceptually complex, it''s surprisingly easy to achieve if you simply read on all of the involved types and method of Reflection in the MSDN articles referenced above and do all steps in the order I explained.

But I want to warn you: you approach might work immediately, but nevertheless, it needs reviewing. You are can get yourself in nightmare when you start modifying the system and support it. All kinds of incompatibilities between versions of software and data in database can haunt you badly. You need to plan for all the scenarios very thoroughly. First of all, revise the whole idea of string class names (or some other code-related entities) in the database. This is potentially serious architecture, needs a serious architect…

Good luck,
—SA


这篇关于如何在Windows应用程序中调用新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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