使用工厂抛出异常进行实例化

Instantiating using factory throwing exception

首先我会说我是新手,所以我可能犯了一个非常简单的错误,但我只是没有看到它。我有以下工厂:

using System;
using System.Collections.Specialized;
using System.Configuration;

namespace SportsCardInventory.Services
{
    public class Factory
    {
        public IService GetService(string servicename)
        {
            Type type;
            Object obj = null;

            try
            {
                string ImplName = GetImplName(servicename);
                type = Type.GetType(ImplName);
                //obj = Activator.CreateInstance(type);
                obj = Activator.CreateInstance(typeof (ICreateCollection));
                return (IService)obj;
            }
            catch (Exception e)
            {
                Console.Write(e);
                throw;
            }
        }

        private string GetImplName(string servicename)
        {
            NameValueCollection settings = ConfigurationManager.AppSettings;
            return settings.Get(servicename);
        }
    }
}

以及以下界面:

using SportsCardInventory.Domain;

namespace SportsCardInventory.Services
{
    interface ICreateCollection:IService
    {
        bool CreateCollection(CardCollection newCollection);
    }
}

以及以下实现:

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using SportsCardInventory.Domain;

namespace SportsCardInventory.Services
{
    class CreateCollectionImpl:ICreateCollection
    {
        public string CollectionName;
        public ArrayList CardCollection;
        public bool CreateCollection(CardCollection newCollection)
        {
            if (newCollection == null) throw new ArgumentNullException("newCollection");
            StoreCollection(newCollection);
            return true;
        }

        private void StoreCollection(CardCollection newCollection)
        {
            string fileName = newCollection.CollectionName + ".bin";
            FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(fileStream, newCollection);
            fileStream.Close();
        }
    }
}

我正在尝试 运行 以下单元测试:

using System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SportsCardInventory.Domain;
using SportsCardInventory.Services;

namespace ServicesTest
{
    [TestClass]
    public class CreateCollectionImplTest
    {
        [TestMethod]
        public void TestMethodCreateCollection()
        {
            bool testPassed;
            ArrayList testArrayList = new ArrayList();
            CardCollection testCollection = new CardCollection("TestCollection", testArrayList);
            Factory factory = new Factory();
            IService createCollectionService = (IService)factory.GetService("ICreateCollection");
        }
    }
}

我收到以下异常:

Test method ServicesTest.CreateCollectionImplTest.TestMethodCreateCollection threw exception: 

System.MissingMethodException: Cannot create an instance of an interface.
    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, ref Boolean canBeCached, ref RuntimeMethodHandleInternal ctor, ref Boolean bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, ref StackCrawlMark stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, ref StackCrawlMark stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at SportsCardInventory.Services.Factory.GetService(String servicename) in Factory.cs: line 25
   at ServicesTest.CreateCollectionImplTest.TestMethodCreateCollection() in CreateCollectionImplTest.cs: line 18

System.MissingMethodException: Cannot create an instance of an interface.
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at SportsCardInventory.Services.Factory.GetService(String servicename) in c:\Users\jfshinevar\Desktop\SCIRefactored\SportsCardInventory\SportsCardInventory\Services\Factory.cs:line 19

我不完全理解正在发生的事情,我觉得我在追逐各种可能性,而每一种都在引发更多问题。有人可以帮助我理解为什么我很难创建 CreateCollectionImpl 的实例,因此我可以对创建集合并将其写入磁盘的能力进行单元测试吗?提前谢谢你。

您只是传递接口名称,您需要传递一个具体的 class 名称来实例化它。

按照您的调用方式,它将尝试单独实例化接口。