다음과 같은 것이 가능한지 궁금합니다. 익명 형식 (문자열, int, decimal, customObject 등)을 허용하는 클래스를 만든 다음 Type을 기반으로 다른 작업을 수행하는 오버로드 된 메서드를 만듭니다. 예형식으로 메서드 오버로드 C#
class TestClass<T>
{
public void GetName<string>()
{
//do work knowing that the type is a string
}
public string GetName<int>()
{
//do work knowing that the type is an int
}
public string GetName<int>(int addNumber)
{
//do work knowing that the type is an int (overloaded)
}
public string GetName<DateTime>()
{
//do work knowing that the type is a DateTime
}
public string GetName<customObject>()
{
//do work knowing that the type is a customObject type
}
}
그래서 지금은 GetName 메서드를 호출 할 수 있고, 내가 객체를 초기화 할 때 이미 유형에 전달하기 때문에, 올바른 방법을 찾아 실행됩니다.
TestClass foo = new TestClass<int>();
//executes the second method because that's the only one with a "int" type
foo.GetName();
이것이 가능한가요 아니면 단지 꿈꿔야할까요?
+1 Generics! = Templates – user7116