我开始说这是我相当新的,所以我可能会犯一个非常简单的错误,但我只是没有看到它。我有以下工厂:
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的实例,所以我可以单元测试创建集合并将其写入磁盘的能力吗?提前谢谢。
答案 0 :(得分:1)
您只是传递了接口名称,您需要传递一个具体的类名来实现它。
您调用它的方式,它将尝试单独实例化界面。