Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "override" an internal class in C# for an open source project

Tags:

c#

I am trying to modify a class that I have obtained from an open source library. While I have referenced the library in my project, the original class contained references to parts of the library that are marked internal. This is causing build errors in my code.

I read this answer: How do you "override" an Internal Class in C#?. Note that it indicates that one is out of luck when wanting to access an internal class when one does not have access to the source code. This is, however, not the case for me.

I can fix this, but I want to know if there is a good way to do it. It appears to me that I have two options. I could modify the source code for the internal class so that it is not internal (bad), or I could import the class itself directly into my project (worse). If I import the class, there are further reference issues, and I will probably have to import the entire library to fix the dependencies.

Note that I am aware that I could add my change to the open source library and then build it as new library to reference in my code. I do not want to do this at this time because I would like to be able to step into my method in the debugger. Once I have debugged my method and assured its utility, I will make it available as part of my version of the open source project.

I am new to c# and this is my first time working on a large open source project so I would like some advise on how to deal with this issue.

Update:

Here is the link to the source code of the library that I would like to modify: https://github.com/accord-net/framework/blob/development/Sources/Accord.Math/Optimization/Unconstrained/Least%20Squares/LevenbergMarquardt.cs

To modify this code, I opened the source in visual studio and attempted to build the version as it currently exists. On line 304 the following code appears:
int[] block = Vector.Range(s * blockSize, s * blockSize + B);.

When I have this file in visual studio, it gives me an error saying that Vector is inaccessible due to its protection level. When I look at the definition of that code in the intellisense window, it shows that Vector is marked as internal.

If I attempt to bring the source code of Vector into my project, there are even more issued of the same type because Vector uses other internal classes.

like image 777
kotval Avatar asked Oct 17 '22 18:10

kotval


1 Answers

Library version:

Are you sure that you're using the latest version of the library ?

By looking at the source of Vector.Range I can see that it is a public member.

Nuget packages of Accord.NET seems up to date, so check out your current version.

Additionally, I would recommend you to depend on the NuGet package instead of a manually installed assembly (you would see update notifications in Nuget Package Manager as a bonus).

Extending the library:

You have a couple of options here,

  • discuss with the authors about the possibility of opening some internal types, explain your needs and they might even suggest an alternative path without making such changes on their side
  • fork the library and make your changes, eventually PR

(personally I'd try step 1 first)

Debugging:

When referencing the Nuget package, you can create a PDB out assembly references using Telerik JustDecompile (freeware). Doing so relieves you to drag your own build and so on.

like image 127
aybe Avatar answered Nov 15 '22 06:11

aybe