次のことが可能かどうか疑問に思っていました。匿名型(文字列、int、decimal、customObjectなど)を受け入れ、その型に基づいて異なる操作を行うオーバーロードされたメソッドを持つクラスを作成します。例タイプでのメソッドのオーバーロード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