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? 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.