Can I rebuild .NET class from reference source?

213 views Asked by At

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?

2

There are 2 answers

4
Scott Chamberlain On BEST ANSWER

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.

2
BTownTKD On

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.