Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting an extension method C#

I am using RestSharp to create http requests to a webservice. One of the parameters length is very long running >100 K characters, so I figured I'll need to use the POST method (because of limitations on length of query string with GET). However, when I tried doing so I got an exception that the uri is too long. I downloaded their source code to find out why. Take a look at the following code:

querystring.AppendFormat("{0}={1}", p.Name.UrlEncode(), p.Value.UrlEncode());

Now the UrlEncode() method is an extension method available in StringExtensions.cs class and it's implementations is like so:

public static string UrlEncode(this string input)
    {
        return Uri.EscapeDataString(input);
    }

The problem is that Uri.EscapeDataString cannot process a string more than 65519 characters (see post - Uri.EscapeDataString() - Invalid URI: The Uri string is too long)

My problem can be solved if the UrlEncode extension method was implemented like this

public static string UrlEncode(this string input)
    {
        int limit = 65520;

        StringBuilder sb = new StringBuilder();
        int loops = input.Length / limit;

        for (int i = 0; i <= loops; i++)
        {
            if (i < loops)
            {
                sb.Append(Uri.EscapeDataString(input.Substring(limit * i, limit)));
            }
            else
            {
                sb.Append(Uri.EscapeDataString(input.Substring(limit * i)));
            }
        }

        return sb.ToString();
    }

The issue is that I DON'T want to HAVE to modify the source code. Is there a way I can write my own extension method in MY source code such that when the third party code is trying to invoke UrlEncode() it ignores it's own extension method and instead calls my extension method??

Any help is much appreciated. Thanks.

like image 510
user2401967 Avatar asked Dec 16 '25 17:12

user2401967


2 Answers

Thankfully, there's no way that I know of. The extension method s.UrlEncode() is basically syntactic sugar for

StringExtensions.UrlEncode(s);

Since this method is static, there's no way to "override" it. In addition, it's bound to that method at compile time, so there's no way to redirect it to a different method at run time.

It should NOT be allowed, either. If it were, You could create an "override" it to format your C drive!.

If YOU want to use a different version, you could create a new extension method with a different name, or figure out a way to shorten your parameter lengths. :)

like image 129
D Stanley Avatar answered Dec 19 '25 13:12

D Stanley


one solution would be to extend the existing Uri class with hiding through inheritance - you should inherit the existing Uri class and then override with the new operator the desired method. This way you will change the default behaviour without modifying original code. Code example:

public class A
{
    public int Get1()
    {
        return 1;
    }

    public int Get2()
    {
        return 100;
    }
}

public class B : A
{
    // override A's Get1
    public new int Get1()
    {
        return 2;
    }
}

and the output of the call:

var b = new B();
System.Console.WriteLine(string.Format("{0} - {1}", b.Get1(), b.Get2()));

would be:

2 - 100

and not 1 - 100!

Hope this helps.

Regards, P.

like image 20
keenthinker Avatar answered Dec 19 '25 11:12

keenthinker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!