[英]C# Autofac- System.MissingMethodException: Cannot create an instance of an interface
[英]System.MissingMethodException: Cannot create an instance of an interface
我有一個帶有幾個文本框的表單,您在文本框中輸入一些值,然后按提交時,將值保存到文件中。 但是,當我按提交時,出現以下異常。 我已經將問題縮小到InventoryMngr和CreateInventory代碼中的某個地方,但是我不確定在那里做錯了什么。
System.MissingMethodException: Cannot create an instance of an interface.
at HomeInventory2.Services.Factory.GetService(String servicename) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Services\Factory.cs:line 37
at HomeInventory2.Business.Manager.GetService(String name) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Business\Manager.cs:line 14
at HomeInventory2.Business.InventoryMngr.Create(CreateInventory inv) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Business\InventoryMngr.cs:line 19
at HomeInventory2.Form1.submitButton_Click(Object sender, EventArgs e) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Form1.cs:line 52
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
庫存庫存
namespace HomeInventory2.Business
{
public class InventoryMngr : Manager
{
/// <summary>
/// persists the inventory information
/// </summary>
/// <param name="inv"></param>
public void Create(CreateInventory inv)
{
InventorySvc inventorySvc =
(InventorySvc)GetService(typeof(InventorySvc).Name);
inventorySvc.CreateInventory(inv);
}
}
}
創建庫存
namespace HomeInventory2.Domain
{
[Serializable]
public class CreateInventory
{
/// <summary>
/// item category
/// </summary>
private string itemCategory;
public String ItemCategory
{
set
{
itemCategory = value;
}
get
{
return itemCategory;
}
}
/// <summary>
/// item properties
/// </summary>
private string itemProperties;
public String ItemProperties
{
set
{
itemProperties = value;
}
get
{
return itemProperties;
}
}
/// <summary>
/// item amount
/// </summary>
private string itemAmount;
public String ItemAmount
{
set
{
itemAmount = value;
}
get
{
return itemAmount;
}
}
/// <summary>
/// item value
/// </summary>
private string itemValue;
public String ItemValue
{
set
{
itemValue = value;
}
get
{
return itemValue;
}
}
}
}
InventorySvc是一個界面
namespace HomeInventory2.Services
{
public interface InventorySvc : IService
{
void CreateInventory(CreateInventory createinventory);
}
}
庫存清單
namespace HomeInventory2.Services
{
public class InventoryImpl: InventorySvc
{
/// <summary>
/// Creates an output files with the given inventory information written to it, serves as placeholder - this will be replaced with a database system
/// </summary>
/// <param name="createinventory"></param>
public void CreateInventory(CreateInventory createinventory)
{
try
{
FileStream fileStream = new FileStream
("CreateInventory.bin", FileMode.Create,
FileAccess.Write);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, createinventory);
fileStream.Close();
}
catch (ItemNotFoundException)
{
throw new ItemNotFoundException("Output not created - see logs");
}
}
}
}
我強烈懷疑您的GetService試圖通過接口名稱創建實例。在.Net中通過接口創建實例是非法的。
您不能創建接口的實例。
在您的create
方法中,將您的inventorySvc
轉換為實現InventorySvc的類
InventorySvc inventorySvc =
(yourClassImplementingInventorySvcInterface)GetService(typeof(InventorySvc).Name);
接口無法實例化。 您應該創建一個實現該接口的類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.