如何强制正确的过载
How to force correct overload
我有两种方法,一种采用对象参数,另一种采用System.Web.UI.Page
类型的参数。我正在发送一个从 System.Web.UI.Page
派生的 class,但被调用的方法是采用 object
参数的方法。
class Primitive
public object Resolve(object FromObject, Sources Source, string Expression)
{
}
public object Resolve(System.Web.UI.Page FromObject, Sources Source, string Expression)
{
}
}
我用我所在的网页来称呼它,它的基数 class 的基数 class 是 System.Web.UI.Page
.
但我从这里调用它:
public Data.CompoundData<Compound<P>, P, object> Resolve(object Source)
{
Data.CompoundData<Compound<P>, P, object> functionReturnValue = default(Data.CompoundData<Compound<P>, P, object>);
functionReturnValue = new Data.CompoundData<Compound<P>, P, object>(this);
foreach (Primitive Primitive in this) {
functionReturnValue.Add(Primitive.Resolve(Source, Primitive.Source, Primitive.SourceExpression));
}
return functionReturnValue;
}
所使用的变量是 object
类型这一事实有所不同。我也不想过载这个调用函数。我必须怎么做才能让这个调用函数调用正确的重载?
注意:如果我删除具有 object
签名的重载,它就可以工作(就像 Page 的情况一样)。但我需要 object
重载,这是对所有其他对象类型的 "catch all" 重载。
谢谢。
显示更多代码,因为这对我有用
namespace WebApplicationOverload
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OverLoad1(new Object());
OverLoad1(new Page());
OverLoad1(new MyPage()); // this call OverLoad1(Page page)
OverLoad1((Page)(new MyPage()));
}
public void OverLoad1(Object obj)
{
if (obj is Page) OverLoad1((Page)obj);
System.Diagnostics.Debug.WriteLine("Object");
}
public void OverLoad1(Page page)
{
System.Diagnostics.Debug.WriteLine("Page");
}
}
public class MyPage : System.Web.UI.Page
{
public MyPage() { }
}
}
此代码无需使用 case 或 if 语句即可解决问题。
GetMethod
在这里做我想做的。它会将我引导至正确的重载,如果不存在特定的重载,则会将我引导至 "catch all" 对象重载。
它的优点是在更多的重载中使用额外的类型时不需要更新;因此,它应该更容易维护。
在我的例子中,使用 Reflection
的任何性能损失都是名义上的,因为我的循环总是有不到一打迭代。
Public Function Resolve(Source As Object) As Data.CompoundData(Of Compound(Of P), P, Object)
Resolve = New Data.CompoundData(Of Compound(Of P), P, Object)(Me)
For Each Primitive As Primitive In Me
Dim MethodInfo As System.Reflection.MethodInfo = GetType(Primitive).GetMethod("Resolve", New Type() {Source.GetType, GetType(Primitive.Sources), GetType(String)})
Resolve.Add(MethodInfo.Invoke(Primitive, New Object() {Source, Primitive.Source, Primitive.SourceExpression}))
Next
End Function
我有两种方法,一种采用对象参数,另一种采用System.Web.UI.Page
类型的参数。我正在发送一个从 System.Web.UI.Page
派生的 class,但被调用的方法是采用 object
参数的方法。
class Primitive
public object Resolve(object FromObject, Sources Source, string Expression)
{
}
public object Resolve(System.Web.UI.Page FromObject, Sources Source, string Expression)
{
}
}
我用我所在的网页来称呼它,它的基数 class 的基数 class 是 System.Web.UI.Page
.
但我从这里调用它:
public Data.CompoundData<Compound<P>, P, object> Resolve(object Source)
{
Data.CompoundData<Compound<P>, P, object> functionReturnValue = default(Data.CompoundData<Compound<P>, P, object>);
functionReturnValue = new Data.CompoundData<Compound<P>, P, object>(this);
foreach (Primitive Primitive in this) {
functionReturnValue.Add(Primitive.Resolve(Source, Primitive.Source, Primitive.SourceExpression));
}
return functionReturnValue;
}
所使用的变量是 object
类型这一事实有所不同。我也不想过载这个调用函数。我必须怎么做才能让这个调用函数调用正确的重载?
注意:如果我删除具有 object
签名的重载,它就可以工作(就像 Page 的情况一样)。但我需要 object
重载,这是对所有其他对象类型的 "catch all" 重载。
谢谢。
显示更多代码,因为这对我有用
namespace WebApplicationOverload
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OverLoad1(new Object());
OverLoad1(new Page());
OverLoad1(new MyPage()); // this call OverLoad1(Page page)
OverLoad1((Page)(new MyPage()));
}
public void OverLoad1(Object obj)
{
if (obj is Page) OverLoad1((Page)obj);
System.Diagnostics.Debug.WriteLine("Object");
}
public void OverLoad1(Page page)
{
System.Diagnostics.Debug.WriteLine("Page");
}
}
public class MyPage : System.Web.UI.Page
{
public MyPage() { }
}
}
此代码无需使用 case 或 if 语句即可解决问题。
GetMethod
在这里做我想做的。它会将我引导至正确的重载,如果不存在特定的重载,则会将我引导至 "catch all" 对象重载。
它的优点是在更多的重载中使用额外的类型时不需要更新;因此,它应该更容易维护。
在我的例子中,使用 Reflection
的任何性能损失都是名义上的,因为我的循环总是有不到一打迭代。
Public Function Resolve(Source As Object) As Data.CompoundData(Of Compound(Of P), P, Object)
Resolve = New Data.CompoundData(Of Compound(Of P), P, Object)(Me)
For Each Primitive As Primitive In Me
Dim MethodInfo As System.Reflection.MethodInfo = GetType(Primitive).GetMethod("Resolve", New Type() {Source.GetType, GetType(Primitive.Sources), GetType(String)})
Resolve.Add(MethodInfo.Invoke(Primitive, New Object() {Source, Primitive.Source, Primitive.SourceExpression}))
Next
End Function