반응형

https://msdn.microsoft.com/ko-kr/library/58918ffs.aspx


형식에 대한 System.Type 개체를 얻는 데 사용됩니다. typeof 식의 형식은 다음과 같습니다.

System.Type type = typeof(int); // type 을 얻어와 인스턴스를 만들 수도 있음 -> http://3dmpengines.tistory.com/1507

식의 런타임 형식을 얻으려면 다음 예제와 같이 .NET Framework 메서드 GetType을 사용합니다.

int i = 0;
System.Type type = i.GetType();

typeof 연산자는 오버로드되지 않습니다.

typeof 연산자는 열린 제네릭 형식에도 사용할 수 있습니다. 형식 매개 변수가 둘 이상인 형식을 지정할 때는 적절한 수의 쉼표를 사용해야 합니다. 다음 예제에서는 메서드의 반환 형식이 제네릭 IEnumerable<T>인지 여부를 확인하는 방법을 보여 줍니다. 메서드는 MethodInfo 형식의 인스턴스라고 가정합니다.

string s = method.ReturnType.GetInterface
    (typeof(System.Collections.Generic.IEnumerable<>).FullName);

public class ExampleClass
{
   public int sampleMember;
   public void SampleMethod() {}

   static void Main()
   {
      Type t = typeof(ExampleClass);
      // Alternatively, you could use
      // ExampleClass obj = new ExampleClass();
      // Type t = obj.GetType();

      Console.WriteLine("Methods:");
      System.Reflection.MethodInfo[] methodInfo = t.GetMethods();

      foreach (System.Reflection.MethodInfo mInfo in methodInfo)
         Console.WriteLine(mInfo.ToString());

      Console.WriteLine("Members:");
      System.Reflection.MemberInfo[] memberInfo = t.GetMembers();

      foreach (System.Reflection.MemberInfo mInfo in memberInfo)
         Console.WriteLine(mInfo.ToString());
   }
}
/*
 Output:
    Methods:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Members:
    Void SampleMethod()
    System.String ToString()
    Boolean Equals(System.Object)
    Int32 GetHashCode()
    System.Type GetType()
    Void .ctor()
    Int32 sampleMember
*/





http://www.lionheart.pe.kr/?document_srl=883&mid=board_uFoa63&listStyle=viewer



유니티 엔진에서  C# 싱글톤

 

public class MyHttp : MonoBehaviour

{


      private static MyHttp s_Instance = null;
 
      public static MyHttp instance 
      {
            get 
            {
                   if (s_Instance == null) 
                   {
                         s_Instance = FindObjectOfType(typeof(MyHttp)) as MyHttp;
                   }

                   // 여전히 null일 경우 새로운 인스턴스 생성
                   if (s_Instance == null) 
                   {
                         GameObject obj = new GameObject("MyHttp");
                         s_Instance = obj.AddComponent(typeof (MyHttp)) as MyHttp;
                   }
                   return s_Instance;
             }
      }
 
      void OnApplicationQuit() 
      {
           s_Instance = null;
      }


}

 

 

유니티 엔진에서  인스턴스 중복 방지

 

public class GameMgr : MonoBehaviour 
{
 
        public static GameMgr instance;
 
        void Awake()
        {
                  if(instance != null)  //  DontDestroyOnLoad(this.gameObject); 인해 해당 오브젝트가 계속 쌓이는 것을 방지
                  {
                            //Destroy(this); // 해당 스크립트를 삭제
                           Destroy(this.gameObject); // 해당 오브젝트를 삭제
                           return;
                  }

                  instance = this;
                 //DontDestroyOnLoad(this);
                 DontDestroyOnLoad(this.gameObject);
                 Application.targetFrameRate = 60; // 최대 프레임은 60으로 지정
        }


}


 

반응형

+ Recent posts