抽象工厂错误CS0246 C#.NET

时间:2017-12-07 13:11:42

标签: c# design-patterns factory cs0246

我有一个工作项目,其中包含诸如计算,串行端口等的单独类。并且希望将它们封装到dll中以用于其他类似项目。我试图实现一个工厂,首先是一个串口工厂,所以我可以在多个dll之间使用。这是我第一次阅读大量的教程并开始使用这个例子:

https://www.codeproject.com/articles/1075518/learning-factory-pattern-using-csharp

它的效果很好,但是我想将FileLogger移到它自己的类中,所以我已经尝试了一段时间,但需要在正确的方向上轻推一下吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPatternSample
{

    interface ILogger
    {
        void Log(string Message);
    }

    enum LoggerType
    {
        File,
        Database,
        EventViewer
    }

    public class File 
    {
        public class FileLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in File.", Message);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FactoryPatternSample
{
    class Program
    {
        File.FileLogger flogger = new File.FileLogger();

        interface ILogger
        {
            void Log(string Message);
        }

        enum LoggerType
        {
            File,
            Database,
            EventViewer
        }



        class DatabaseLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in Database.", Message);
            }
        }

        class EventViewerLogger : ILogger
        {
            public void Log(string Message)
            {
                Console.WriteLine("{0} - Logged in EventViewer.", Message);
            }
        }

        class LoggerFactory
        {
            public static ILogger Get(LoggerType type)
            {
                switch (type)
                {
                    case LoggerType.Database:
                        return new DatabaseLogger();
                    case LoggerType.EventViewer:
                        return new EventViewerLogger();
                    case LoggerType.File:
                    default:
                        return new flogger();
                }
            }
        }

        static void Main(string[] args)
        {
            ILogger logger1 = LoggerFactory.Get(LoggerType.Database);
            logger1.Log("Message from Main");

            ILogger logger2 = LoggerFactory.Get(LoggerType.File);
            logger2.Log("Message from Main");

            ILogger logger3 = LoggerFactory.Get(LoggerType.EventViewer);
            logger3.Log("Message from Main");

            Console.ReadKey();

            /*Output
            Message from Main - Logged in Database.
            Message from Main - Logged in File.
            Message from Main - Logged in EventViewer.
            */
        }
    }
}

这一位是导致问题的原因:

return new flogger();

这是错误:

错误CS0246类型或命名空间名称' flogger'找不到(你错过了使用指令或汇编引用吗?)

我无法让它引用File.FileLogger,请帮忙吗?

1 个答案:

答案 0 :(得分:0)

解决了,一旦我删除了:

src

来自程序类,结束了它现在可以工作,因为我有两个接口