2

我有一个包含几个文本框的表单,您在文本框中输入一些值,然后当您按下提交时,它将值保存到文件中。但是,当我按下提交时,我得到以下异常。我已将问题缩小到 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);
    }
}

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

3 回答 3

1

我强烈怀疑您的 GetService 正在尝试使用接口名称创建实例。在 .Net 中从接口创建实例是非法的。

于 2012-10-20T17:31:05.160 回答
0

您不能创建接口的实例..

在您的create方法中,将您的inventorySvc转换为实现 InventorySvc 的类

  InventorySvc inventorySvc = 
        (yourClassImplementingInventorySvcInterface)GetService(typeof(InventorySvc).Name); 
于 2012-10-20T17:28:36.217 回答
-1

接口无法实例化。您应该创建一个实现该接口的类。

于 2012-10-20T17:23:37.923 回答