0

How to implement an extension method that can change result of ToString() of any object?

What I have now:

public class ProxyBase
{
    public override string ToString()
    {
        return "hardcodedValue"
    }
}
public static T OverrideToString<T>(this T ob, message) where T : class
{
    var g = new ProxyGenerator();
    var o = new ProxyGenerationOptions();
    o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
    // how to use message parameter here?
    return g.CreateInterfaceProxyWithTarget(ob, o);
}

I can't change the hardcodedValue at runtime

user963935
  • 493
  • 4
  • 20
  • Why somebody has downvoted? – user963935 Oct 25 '19 at 06:04
  • I didn't, but you might want to mention what research you have done in an attempt to solve this problem. – ProgrammingLlama Oct 25 '19 at 06:06
  • 3
    Thank you for taking the time to share your problem. What you asking for is unclear. What is your goal and your difficulty? What have you done so far? Please try to better explain your issue, your development environment and the data structures, as well as to share more code (no screenshot), images or sketches of the screen, and user stories or scenario diagrams. To help you improve your requests, please read the *[How do I ask a good question](https://stackoverflow.com/help/how-to-ask)* and **Questions I avoid asking** at the top right. –  Oct 25 '19 at 06:09
  • Edited to resolve all the concerns – user963935 Oct 25 '19 at 06:15
  • Is it a duplicate of https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method ? – Caius Jard Oct 25 '19 at 06:20
  • At Runtime you can create a new class, derived from your ProxyBase, and override the ToString() Method. It's the same thing what you can do when writing source code. Check 'TypeBuilder' for samples. – Holger Oct 25 '19 at 08:42

2 Answers2

1

Here is the answer:

public class ProxyBase
{
    public string Message { get; set; }

    public override string ToString()
    {
        return Message;
    }
}

public static T OverrideToString<T>(this T ob, message) where T : class
{
    var g = new ProxyGenerator();
    var o = new ProxyGenerationOptions();
    o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
    var proxied = g.CreateInterfaceProxyWithTarget(ob, o);
    var baseProxy = proxied as ProxyBase;
    baseProxy.Name = message;
    return proxied;
}

The main use case are parametrized XUnit test, where test runner uses default ToString() method for displaying parameters. When the parameter class is not from our code base and we can't inherit it, this extension may be used to give it a friendly name.

user963935
  • 493
  • 4
  • 20
-1

If you change the "hardcodedvalue" into a variable with onpropertychanged, you can set the variable and the ToString() will output different things.

     private string yourToStringResult
     public string YourToStringResult
     {
         get { return yourToStringResult; }
         set
         {
             yourToStringResult = value;
             OnPropertyChanged("YourToStringResult");
         }
     }

If you return YourToStringResult in your ToString, you can change the value that your ToString returns.

Zen Zac
  • 136
  • 1
  • 9