简体   繁体   English

使用工厂抛出异常实例化

[英]Instantiating using factory throwing exception

I'll start this by saying I'm am fairly new, so I might be making a very simple mistake, but I'm just not seeing it. 首先,我要说我还很新,所以我可能犯了一个非常简单的错误,但我只是没有看到它。 I have the following factory: 我有以下工厂:

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

And the following interface: 和以下界面:

using SportsCardInventory.Domain;

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

And the following implementation: 以及以下实现:

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

I am trying to run the following Unit Test: 我正在尝试运行以下单元测试:

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

And I am getting the following exception: 我收到以下异常:

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

I don't fully understand what is occurring and I feel like I am chasing possibilities and each one is causing more issues. 我不完全了解正在发生的事情,我觉得自己在追寻可能性,而每个问题都在引起更多问题。 Can someone help me understand why I am having a hard time creating an instance of CreateCollectionImpl, so I can unit test the ability to create a collection and write it to disk? 有人可以帮助我理解为什么我很难创建CreateCollectionImpl实例,以便可以对创建集合并将其写入磁盘的能力进行单元测试吗? Thank you in advance. 先感谢您。

You're just passing the interface name, you'll need to pass a concrete class name to instatiate it. 您只需要传递接口名称,就需要传递一个具体的类名称以使其实例化。

The way you're calling it, it will try to instantiate the interface alone. 调用方式将尝试单独实例化该接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM