找不到类型或命名空间名称“T”(您是否缺少using指令或程序集引用?)

时间:2012-04-20 04:43:26

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

我正在试用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"

        }
    }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

public abstract class SingletonBase<t> where T : class

应该是:

public abstract class SingletonBase<T> where T : class

C#区分大小写,因此编译器将where T : class视为引用未知泛型类型参数,因为您在类型转换中使用了小写t SingletonBase<t>