Programing

제네릭 유형이 문자열인지 테스트하는 가장 좋은 방법은 무엇입니까?

lottogame 2020. 9. 9. 20:07
반응형

제네릭 유형이 문자열인지 테스트하는 가장 좋은 방법은 무엇입니까? (씨#)


모든 유형, 기본 또는 기타를 허용해야하는 제네릭 클래스가 있습니다. 이것의 유일한 문제는 default(T). 값 유형이나 문자열에서 default를 호출하면 적절한 값 (예 : 빈 문자열)으로 초기화됩니다. default(T)객체 를 호출하면 null을 반환합니다. 여러 가지 이유로 기본 유형이 아닌 경우 null이 아닌 유형의 기본 인스턴스가 있는지 확인해야합니다 . 다음은 시도 1입니다.

T createDefault()
{
    if(typeof(T).IsValueType)
    {
        return default(T);
    }
    else
    {
        return Activator.CreateInstance<T>();
    }
}

문제-문자열이 값 유형이 아니지만 매개 변수가없는 생성자가 없습니다. 따라서 현재 솔루션은 다음과 같습니다.

T createDefault()
{
    if(typeof(T).IsValueType || typeof(T).FullName == "System.String")
    {
        return default(T);
    }
    else
    {
        return Activator.CreateInstance<T>();
    }
}

그러나 이것은 kludge처럼 느껴집니다. 문자열 케이스를 처리하는 더 좋은 방법이 있습니까?


default (string)은 string이 아니라 null입니다. 코드에서 특별한 경우를 원할 수 있습니다.

if (typeof(T) == typeof(String)) return (T)(object)String.Empty;

if (typeof(T).IsValueType || typeof(T) == typeof(String))
{
     return default(T);
}
else
{
     return Activator.CreateInstance<T>();
}

테스트되지 않았지만 가장 먼저 떠오른 것이 있습니다.


TypeCode 열거를 사용할 수 있습니다 . IConvertible 인터페이스를 구현하는 클래스에서 GetTypeCode 메서드를 호출하여 해당 클래스의 인스턴스에 대한 형식 코드를 가져옵니다. IConvertible은 Boolean, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Double, Decimal, DateTime, Char 및 String으로 구현되므로이를 사용하여 기본 형식을 확인할 수 있습니다. " 일반 유형 검사 " 에 대한 자세한 정보 .


개인적으로 메서드 오버로딩을 좋아합니다.

public static class Extensions { 
  public static String Blank(this String me) {      
    return String.Empty;
  }
  public static T Blank<T>(this T me) {      
    var tot = typeof(T);
    return tot.IsValueType
      ? default(T)
      : (T)Activator.CreateInstance(tot)
      ;
  }
}
class Program {
  static void Main(string[] args) {
    Object o = null;
    String s = null;
    int i = 6;
    Console.WriteLine(o.Blank()); //"System.Object"
    Console.WriteLine(s.Blank()); //""
    Console.WriteLine(i.Blank()); //"0"
    Console.ReadKey();
  }
}

String에 대한 논의는 여기서 작동하지 않습니다.

제네릭이 작동하도록하려면 다음 코드가 필요했습니다.

   private T createDefault()
    { 

        {     
            if(typeof(T).IsValueType)     
            {         
                return default(T);     
            }
            else if (typeof(T).Name == "String")
            {
                return (T)Convert.ChangeType(String.Empty,typeof(T));
            }
            else
            {
                return Activator.CreateInstance<T>();
            } 
        } 

    }

참고 URL : https://stackoverflow.com/questions/31498/best-way-to-test-if-a-generic-type-is-a-string-c

반응형