5

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.

Community
  • 1
  • 1
kotval
  • 120
  • 2
  • 10
  • 4
    Internal classes can only be accessed from within that library. You will have to change the library in order to access the internal class. I think the creator did make it internal for a reason. Perhaps the creator wants to make sure the library is easy in use, by not making some classes available. – Max Jan 21 '17 at 19:19
  • Neither of your approaches are good ... I think you should update your question with an example of what you're trying to do with relevant urls to the open source project, people could then suggest something (it's hard to give an advice without knowing much). – aybe Jan 21 '17 at 19:28
  • @Max It is surely the case that the creator's goal in making this class `internal` was to make the library easy to use. Class that I would like to access would be of little use except within the methods of the library. My question is really, though, since I am modifying a method that is in the library, what it is the appropriate way to change the library so that I do have access to this `internal` class? Do I have to import the source code into my project directly? There must be a better way. – kotval Jan 21 '17 at 19:30
  • @Aybe I will do that. – kotval Jan 21 '17 at 19:31
  • @kotval Is the project hosted on Github? If so, you could fork and change it. You will need to merge every new library update into your fork, in order to stay updated. Another option would be submitting a pull request to the library, allowing others to make use of this external class or making it available using the public api. – Max Jan 21 '17 at 19:36
  • @Max It is posted on Github. I have forked it. Forgive my ignorance, but I copied the source code to my visual studio so that I might be able to step through it with the debugger. If I just do my modifications on my own fork, then download it, I don't believe that I would be able to step through it with the debugger. If I am wrong, please educate me. – kotval Jan 21 '17 at 19:46

1 Answers1

3

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.

aybe
  • 15,516
  • 9
  • 57
  • 105
  • Thanks so much! You were right about the library version. For some reason, I had an older version of the library. I updated it with NuGet, and the Vector.Range class became public. Additionally, Thanks for letting me know about Telerik JustDecompile. That will make my life easier in the future. – kotval Jan 21 '17 at 20:27