Skip to content

这种类没有主动加文件头 #4

@sonygod

Description

@sonygod

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class Singleton<T> where T :  ISingeton
{
    private static T mUniqueInstance;


    public static bool Exists
    {
        get { return Singleton<T>.mUniqueInstance != null; }
    }

    public static T Instance
    {
        get
        {
            if (!Exists)
            {
                // Debug.Log("不存在,创建!");

                Singleton<T>.mUniqueInstance = jsimp.Reflection.CreateInstance<T>();
                    //Activator.CreateInstance<T>();
            }
            // Debug.Log("创建后" + Singleton<T>.mUniqueInstance);

            return Singleton<T>.mUniqueInstance;
        }
    }

    static Singleton()
    {
    }

    public Singleton()
    {
        if (Exists)
        {
            throw new InvalidOperationException("can not be instance like this");
        }
    }

    public static void Release()
    {
        if (Exists)
        {
            Singleton<T>.mUniqueInstance.Clean();
            Singleton<T>.mUniqueInstance = default(T);
        }
    }
}

public interface ISingeton


{
    void Clean();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions