我想知道以下是否可能。创建一个接受匿名类型(string,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!=模板 – user7116 2009-10-22 17:11:56