【问题标题】:The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)找不到类型或命名空间名称“T”(您是否缺少 using 指令或程序集引用?)
【发布时间】:2012-05-01 16:39:21
【问题描述】:

我正在尝试来自singleton base class article 的一些代码。但是当我编译时,它给了我那个错误信息。我确保项目的目标框架是完整的 4.0,而不是 4.0 客户端框架。代码有什么问题?

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Reflection;

namespace ConsoleApplication1
{
    public abstract class SingletonBase<t> where T : class
    {
        /// <summary>
        /// A protected constructor which is accessible only to the sub classes.
        /// </summary>
        protected SingletonBase() { }

        /// <summary>
        /// Gets the singleton instance of this class.
        /// </summary>
        public static T Instance
        {
            get { return SingletonFactory.Instance; }
        }

        /// <summary>
        /// The singleton class factory to create the singleton instance.
        /// </summary>
        class SingletonFactory
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static SingletonFactory() { }

            // Prevent the compiler from generating a default constructor.
            SingletonFactory() { }

            internal static readonly T Instance = GetInstance();

            static T GetInstance()
            {
                var theType = typeof(T);

                T inst;

                try
                {
                    inst = (T)theType
                      .InvokeMember(theType.Name,
                        BindingFlags.CreateInstance | BindingFlags.Instance
                        | BindingFlags.NonPublic,
                        null, null, null,
                        CultureInfo.InvariantCulture);
                }
                catch (MissingMethodException ex)
                {
                    throw new TypeLoadException(string.Format(
                      CultureInfo.CurrentCulture,
                      "The type '{0}' must have a private constructor to " +
                      "be used in the Singleton pattern.", theType.FullName)
                      , ex);
                }

                return inst;
            }
        }
    }

    public sealed class SequenceGeneratorSingleton : SingletonBase<SequenceGeneratorSingleton>
    {
        // Must have a private constructor so no instance can be created externally.
        SequenceGeneratorSingleton()
        {
            _number = 0;
        }

        private int _number;

        public int GetSequenceNumber()
        {
            return _number++;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Sequence: " + SequenceGeneratorSingleton.Instance
                .GetSequenceNumber().ToString());  // Print "Sequence: 0"

        }
    }
}

【问题讨论】:

    标签: c# visual-studio visual-studio-2010 singleton


    【解决方案1】:

    t的情况吗?应该是T。无论如何,它需要在整个班级中保持一致,并且大写是惯例。

    【讨论】:

      【解决方案2】:
      public abstract class SingletonBase<t> where T : class
      

      应该是:

      public abstract class SingletonBase<T> where T : class
      

      C# 区分大小写,因此编译器将 where T : class 视为引用未知的泛型类型参数,因为您在类型声明中使用了小写的 t,SingletonBase&lt;t&gt;

      【讨论】:

        猜你喜欢
        • 2013-08-28
        • 1970-01-01
        • 1970-01-01
        • 2020-11-28
        • 2015-06-12
        • 2015-04-19
        • 2013-05-22
        • 2020-03-29
        • 2021-02-27
        相关资源
        最近更新 更多