如何使用C#.net将字符串转换为对象 [英] how to convert string to object using C# .net

查看:55
本文介绍了如何使用C#.net将字符串转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dim frm As New Form
        Dim formName As String = "Form1"
        formName = [Assembly].GetEntryAssembly.GetName.Name & "." & formName
        frm = DirectCast([Assembly].GetEntryAssembly.CreateInstance(formName), Form)
        frm.Show()

//how to use this in c#

推荐答案

您的意思是将其转换为C#?



Do you mean convert it to C#?

Form frm = new Form();
string formName = "Form1";
formName = string.Format("{0}.{1}", [Assembly].GetEntryAssembly.GetName.Name, formName);
frm = [Assembly].GetEntryAssembly.CreateInstance(formName) as Form;
frm.Show();





或更简洁:





Or more succinctly:

string name = string.Format("{0}.Form1", [Assembly].GetEntryAssembly.GetName.Name);
Form frm = [Assembly].GetEntryAssembly.CreateInstance(formName) as Form;
frm.Show();


string formName = "frmTestForm";
formName = Assembly.GetEntryAssembly().GetName().Name + "." + formName;
               
Type type = Type.GetType(formName);
Form form = (Form)Activator.CreateInstance(type);

form.StartPosition = FormStartPosition.CenterParent;
form.ShowDialog();


这篇关于如何使用C#.net将字符串转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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