开发者

Call overridden function from a System.Data.Entity.DynamicProxies object

开发者 https://www.devze.com 2023-03-28 23:43 出处:网络
I have an extension method I\'m trying to make generic, for message passing. public static ActionResult RedirectCompanyMessage<T>(this Controller controller, T mObject, Company company)

I have an extension method I'm trying to make generic, for message passing.

    public static ActionResult RedirectCompanyMessage<T>(this Controller controller, T mObject, Company company)
    {
        var msg = mObject.ToString()
        ...
    }

the mObject.ToString() is returning junk like System.Data.Entity.DynamicProxies.SerialCode开发者_C百科_960A5FEF6FE5426EE5F55B8627454C71E7D088921143DE49B208E7FED043ADA5

But, the base type (the proxy type?) has an overridden ToString().

public partial class SerialCode
{
    //Prints the serial code with dashes every 5 chars
    public new String ToString()
    {
        return Utility.Utility.FormatSerial(this.Serial);
    }
}

So, what's the deal? During debugging, if I hover over "T", visual studio shows T mObject as the correct Models.SerialCode, but if I run mObject.GetType() in the immediate window I see FullName = "System.Data.Entity.DynamicProxies.SerialCode_960A5F...

I just want to be able to reliably run the overridden ToStrings() in all my partial classes.


The problem is that you didn't "override" .ToString(); you reintroduced it with new. If you want to override, use override, not new.

0

精彩评论

暂无评论...
验证码 换一张
取 消