C# 根据给定接口的字符串实例化类
我试图根据将从用户界面检索的字符串创建一个类的实例,然后我想访问该类实例的属性。
以下是我迄今为止所掌握的内容的概述 -
namespace MamdaAdapter
{
public interface IExchange
{
string GetTransport();
}
}
namespace MamdaAdapter
{
public class Exchange
{
public class Arca : IExchange
{
private const string _Transport = "tportname";
public string GetTransport()
{
return _Transport;
}
}
public static IExchange DeriveExchange(string ExchangeName)
{
IExchange SelectedExchange = (IExchange)Activator.CreateInstance(Type.GetType(ExchangeName));
return SelectedExchange;
}
}
}
namespace MyUserInterface
{
public class MainForm
{
private void simpleButton1_Click(object sender, EventArgs e)
{
IExchange SelectedExchange = Exchange.DeriveExchange("Exchange.Arca");
Console.WriteLine(SelectedExchange.GetTransport());
}
}
}
更新: 现在,我收到一个异常,提示“值不能为空”,这对我来说意味着它无法根据提供的字符串创建类的实例 -
I am trying to make an instance of a class based on a string that will be retrieved from the User Interface, and then I want to access the properties of the instance of the class.
Here is an overview of what I have so far -
namespace MamdaAdapter
{
public interface IExchange
{
string GetTransport();
}
}
namespace MamdaAdapter
{
public class Exchange
{
public class Arca : IExchange
{
private const string _Transport = "tportname";
public string GetTransport()
{
return _Transport;
}
}
public static IExchange DeriveExchange(string ExchangeName)
{
IExchange SelectedExchange = (IExchange)Activator.CreateInstance(Type.GetType(ExchangeName));
return SelectedExchange;
}
}
}
namespace MyUserInterface
{
public class MainForm
{
private void simpleButton1_Click(object sender, EventArgs e)
{
IExchange SelectedExchange = Exchange.DeriveExchange("Exchange.Arca");
Console.WriteLine(SelectedExchange.GetTransport());
}
}
}
UPDATE:
Right now, I'm getting an Exception that says the "Value cannot be null" which to me means that it is unable to create the instance of the class given the string provided -
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的问题是如何指定类的名称:
首先,指定名称空间。其次,由于 Arca 是一个内部类,因此您必须使用“+”而不是“.”。
The problem here is how you specify the name of your class:
First, specify the namespace. Second, since Arca is an inner class you must use '+' instead of '.'
假设您的 UI 没有公开完整的类型名称,您通常需要一个字典将显示名称与类型相关联:
然后,您只需实例化新对象:
如果在编译时未知字符串,则基本上需要检查类型的存在:
Assuming you UI doesnt expose the full type name, you typically want a dictionary to associate the display name to the type:
Then, you simply instantiate the new object:
If the string is not known at compile time, you basically need to check for the existance of the type:
我编写了一个小型的 C# 控制台应用程序来模拟您的需求,测试正常,希望它有所帮助:
I wrote a small c# console application to simulate your need, tested ok, hope it helps:
如果您要查找的类型未在执行 Type.GetType 的同一程序集中定义,则必须使用 AssemblyQualifiedName(例如 MyNamespace.MyClass、MyAssembly、Version=1.3.0.0、Culture=neutral、PublicKeyToken=b17a5c561934e089),甚至全名是不够的。否则,您可以先获取包含该类的程序集,然后执行 Assembly 类的 GetType 方法。
If the Type you are looking for is not defined in the same assembly that is executing Type.GetType you must use the AssemblyQualifiedName (something like MyNamespace.MyClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089), even the FullName is not enough. Otherwise you could first get the assembly containing the class and then execute the GetType method of the Assembly class.