以下が可能かどうか疑問に思っていました。匿名型 (string、int、decimal、customObject など) を受け入れるクラスを作成し、Type に基づいてさまざまな操作を行うメソッドをオーバーロードします。例
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();
これは可能ですか、それとも私はただ夢を見ているだけですか?