2

I want to add some methods in System.Net.HttpWebRequest class to suit my needs. I tried reflection but it is quite complicated that I need to alter many of its member class method as well.

I am debugging through .NET reference source and I could view the source code of those class. Is it possible for me to copy each of the related class source code and build my own class?

Isolet Chan
  • 417
  • 6
  • 20
  • 2
    Are you aware of [Extension Methods](https://msdn.microsoft.com/en-GB/library/bb383977.aspx)? They may provide a way for you to "extend" the class in the way you need. – James Thorpe Nov 24 '15 at 15:26
  • 2
    `HttpWebRequest` isn't `sealed`, just derive from it and add whatever you like. You can re-implement by using method hiding if needed. – Ron Beyer Nov 24 '15 at 15:28
  • I aware of extension method but since the class involved many internal stream class. I will need a lot of reflection code to add my desired behaviour to HttpWebRequest class – Isolet Chan Nov 24 '15 at 15:39

2 Answers2

4

For some classes yes, but for many no.

.NET classes frequently use internal classes that are not exposed publicly, you would not only need to rebuild the class you are interested in but also rebuild all internal references too.

I would recommend not trying to do this and instead either using Extension Methods or if that does not solve your problem ask a new question describing the exact thing you are trying to accomplish and perhaps we can show you a easier way to do it.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Since the class involve many internal class, extension method cannot solve it. With the reference source debugging enabled, I seems to be able to view any internal class as well. I am wondering if those symbol files(.pdb) are exactly the source code? Arent they only for the debugging purpose? – Isolet Chan Nov 24 '15 at 15:35
  • 2
    You never told us what you are trying to actually do so we can't say if a Extension method would solve it or not. I am 100% sure whatever problem you are actually facing can be solved without re-implementing HttpWebRequest. – Scott Chamberlain Nov 24 '15 at 15:39
  • Like I said, I would recommend you ask a new question and go in to detail what you want to accomplish and show what you have tried so far. This question was "Can I rebuild .NET class from reference source?" and you got your answer "Yes but it will be a PITA". You are unlikely to get meaningful help by asking in the comments. – Scott Chamberlain Nov 24 '15 at 16:02
  • Thank you for your help! I will try to rebuild it first and ask if I failed – Isolet Chan Nov 24 '15 at 16:05
2

Anything's possible.

You have the source code. You know how to copy and paste. Certainly it's possible you could adapt that code for your own purposes.

The question is; is it legal?

To answer that, you need only examine the license for the reference source. To that end, it's perfectly legal, assuming you comply with the MIT license (which is pretty lenient).

The next question is; should you? Probably not. Most likely, you could just add your desired functionality via a helper class or child class, or add new methods via Extension Methods.

BTownTKD
  • 7,911
  • 2
  • 31
  • 47
  • I am not sure if I am having the source code. I cannot see the System.Net.HttpWebRequest in online reference source but I can debug via it if I open the debugging option on vs to download the symbol file from microsoft server. – Isolet Chan Nov 24 '15 at 15:54
  • 1
    @IsoletChan The source code is available here: http://referencesource.microsoft.com/#System/net/System/Net/HttpWebRequest.cs,f2ab2d685cb26f13 – Robert McKee Nov 24 '15 at 17:45