简体   繁体   English

System.MissingMethodException:无法创建接口的实例

[英]System.MissingMethodException: Cannot create an instance of an interface

I have a form that has a few text boxes, you input some values into the text boxes and then when you press submit it saves the values to a file. 我有一个带有几个文本框的表单,您在文本框中输入一些值,然后按提交时,将值保存到文件中。 However when I press submit, I get the following exception. 但是,当我按提交时,出现以下异常。 I've narrowed the problem down to something in the InventoryMngr and CreateInventory code, but I am unsure what I am doing wrong there. 我已经将问题缩小到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)

InventoryMngr 库存库存

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

CreateInventory 创建库存

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 is an interface InventorySvc是一个界面

namespace HomeInventory2.Services
{
    public interface InventorySvc : IService
    {
        void CreateInventory(CreateInventory createinventory);
    }
}

InventoryImpl 库存清单

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中通过接口创建实例是非法的。

You cannot create an instance of an interface.. 您不能创建接口的实例。

in your create method, cast your inventorySvc to class which implements InventorySvc 在您的create方法中,将您的inventorySvc转换为实现InventorySvc的类

  InventorySvc inventorySvc = 
        (yourClassImplementingInventorySvcInterface)GetService(typeof(InventorySvc).Name); 

Interface cannot be instantiated. 接口无法实例化。 You should create a class that implements the interface. 您应该创建一个实现该接口的类。

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

相关问题 C# Autofac- System.MissingMethodException:无法创建接口的实例 - C# Autofac- System.MissingMethodException: Cannot create an instance of an interface 温莎城堡+ ASP MVC 5-System.MissingMethodException:无法创建接口的实例 - Castle Windsor + ASP MVC 5 - System.MissingMethodException: Cannot create an instance of an interface System.MissingMethodException:无法创建抽象类 - System.MissingMethodException: Cannot create an abstract class Activator.CreateInstance() 在创建实例时抛出 System.MissingMethodException C# - Activator.CreateInstance() throw System.MissingMethodException while create the instance C# ASP Net Core MVC创建实例控制器System.MissingMethodException:&#39;为此对象未定义无参数构造函数。 - asp net core mvc create instance controller System.MissingMethodException: 'No parameterless constructor defined for this object.' TryUpdateModel和System.MissingMethodException - TryUpdateModel and System.MissingMethodException System.MissingMethodException错误 - System.MissingMethodException error System.MissingMethodException - System.MissingMethodException EF System.MissingMethodException - EF System.MissingMethodException “System.MissingMethodException未处理”? - “System.MissingMethodException was unhandled”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM