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());
        }
    }
}

更新:现在,我得到一个异常,说"值不能为空",这对我来说意味着它是无法创建类的实例给定字符串提供-

这里的问题是如何指定类名:

首先,指定命名空间。其次,由于Arca是一个内部类,你必须使用'+'而不是'。'

(...) = Exchange.DeriveExchange("MamdaAdapter.Exchange+Arca");

假设您的UI没有公开完整的类型名称,您通常需要一个字典将显示名称与类型关联起来:

Dictionary<string, Type> _associations = new Dictionary<string, Type>(); 

然后,简单地实例化新对象:

if(_associations.ContainsKey(someString))
{
   Type selectedType = _associations[someString];
   return Activator.CreateInstance(selectedType) as IExchange;
}
throw new ApplicationException("No type defined for that string yo");

如果在编译时不知道该字符串,则基本上需要检查类型是否存在:

var type = Type.GetType(someString);
if(type != null)
{
    // Do Stuff
}

我写了一个小的c#控制台应用程序来模拟您的需要,经过测试还可以,希望对您有所帮助:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MamdaAdapter;
using System.Reflection;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IExchange SelectedExchange = Exchange.DeriveExchange("MamdaAdapter.Arca");
            Console.WriteLine(SelectedExchange.GetTransport());
        }
    }
}
namespace MamdaAdapter
{
    public interface IExchange
    {
        string GetTransport();
    }
}

namespace MamdaAdapter
{
    public class Arca : IExchange
    {
        private const string _Transport = "tportname";
        public string GetTransport()
        {
            return _Transport;
        }
    }
}
namespace MamdaAdapter
{
    public class Exchange
    {
        public static IExchange DeriveExchange(string ExchangeName)
        {
            IExchange SelectedExchange = (IExchange)Assembly.GetAssembly(typeof(IExchange)).CreateInstance(ExchangeName, false, BindingFlags.CreateInstance, null, null, null, null);
            return SelectedExchange;
        }
    }
}

如果您正在查找的类型未在执行Type的同一程序集中定义。GetType必须使用AssemblyQualifiedName(类似于MyNamespace)。MyClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089),甚至FullName也不够。否则,您可以先获取包含该类的程序集,然后执行该assembly类的GetType方法。

相关内容

  • 没有找到相关文章

最新更新