你怎么能转换为使用类型名称作为字符串类型? [英] How can you cast to a type using the type name as a string?

查看:114
本文介绍了你怎么能转换为使用类型名称作为字符串类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我敲敲这个想法一整天,现在,我已经达到了,我承认我只是平了不知道的部分。这可能是我在做什么仅仅是愚蠢的,有一个更好的办法,但是这是我的想法给我带来了。

Ok, I've thumped on this idea all day now, and I have reached the part where I admit I just flat out don't know. It's possible that what I'm doing is just stupid and there is a better way, but this is where my thinking has brought me.

我试图用一个通用方法加载形式的WinForms:

I am attempting to use a generic method to load forms in WinForms:

protected void LoadForm<T>(ref T formToShow, bool autoLoaded) where T : FormWithWorker, new()
{
    // Do some stuff
}

的形式由ToolStripMenuItem(无论是通过项目的选择或使用打开Windows菜单项)加载。他们是懒加载的,所以有MDI父内的表单字段,但它们是空的,直至需要它们。我已经使用ToolStripMenuItem_Click常用的方法处理所有菜单项的点击。该方法具有不知道是被要求除了ToolStripMenuItem的名称选择了它们所对应的窗体类名的模式匹配何种形式的没有真正的方法。因此,使用ToolStripMenuItem的名字,我可以占卜所请求形式的类型和分配给存储表单中的参考私有字段的名称的名称。

The forms are loaded by a ToolStripMenuItem (either through the selection of the item or using the Open Windows menu item). They are lazy-loaded, so there are fields for the forms within the MDI parent, but they are null until they are needed. I have a common method used for ToolStripMenuItem_Click that handles all of the menu item clicks. The method has no real way of knowing which form is being called for except that the name of the ToolStripMenuItem matches a pattern chosen for the form class names they correspond to. So, using the name of the ToolStripMenuItem, I can divine the name of the type of form being requested and the name of the private field allocated to store the reference for that form.

利用这一点,我可以使用一个不断增长/收缩switch语句通过硬编码类型和字符串匹配调用方法与特定类型集(不良),或者我可以使用反射来获取领域,并创建的实例类型。我的问题是, System.Activator.CreateInstance 规定,不能转换为我需要的类型的ObjectHandler。这里是什么我到目前为止的代码段:

Using that, I can either use a growing/contracting switch statement with hard-coded types and string matches to call method with the specific type set (undesirable), or I can use Reflection to get the field and create the instance of the type. The problem to me is, System.Activator.CreateInstance provides an ObjectHandler that can't be cast to the types that I need. Here is a snippet of what I have so far:

string formName = "_form" + ((ToolStripMenuItem)sender).Name.Replace("ToolStripMenuItem", "");
string formType = formName.Substring(1);

FieldInfo fi = this.GetType().GetField(formName, BindingFlags.NonPublic | BindingFlags.Instance);

FormWithWorker formToLoad = (FormWithWorker)fi.GetValue(this);
if (formToLoad == null)
{
    formToLoad = (????)System.Activator.CreateInstance("MyAssemblyName", formType);
}

this.LoadForm(ref formToLoad, false);
fi.SetValue(this, formToLoad);



我知道,去为(????),但在编译类型的字符串名称 - 时间我不知道是什么类型,因为它的变化。我已经尝试了一堆的方式来获得这个转换/实例化工作,但都没有成功。我非常想知道是否有可能进行这样的铸知道只有类型为字符串。我试着用 Type.GetType(字符串,字符串)来执行转换,但是编译器不喜欢它。如果有人对如何动态加载的形式,因为我只是做愚蠢不同的想法,请让我知道。

I know the string name of the type that goes in for (????) but at compile-time I do not know the type because it changes. I have tried a bunch of ways to get this cast/instantiation to work, but none have been successful. I would very much like to know if it's possible to perform such a cast knowing the type only as a string. I tried using Type.GetType(string, string) to perform the cast, but the compiler didn't like it. If someone has a different idea on how to load the forms dynamically because I'm just doing it stupidly, please let me know about it.

推荐答案

你会与其他超载更好了需要键入并使用如 Type.GetType(字符串)

You'd be better off with the other overload that takes a Type and using e.g. Type.GetType(string).

FormWithWorker formToLoad = (FormWithWorker)fi.GetValue(this);
if (formToLoad == null)
{
    formToLoad =
      (FormWithWorker)System.Activator.CreateInstance(Type.GetType("MyNamespace.MyFormType"));
}

这篇关于你怎么能转换为使用类型名称作为字符串类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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