Fixing Common dnSpy Errors

Identifier Expected (base.ctor)
You will often get this error when editing a class constructor. Within the method, you should find a line that looks like:

base..ctor(...);

You need to move this call up next to the constructor signature. So if you have:

public SomeClass(x, y, z) { ...   base..ctor(x, y, z); ... }

You should change it to:

public SomeClass(x, y, z) : base(x, y, z) { ... }

The type X already contains a definition for Y
(The newest nightly builds of dnSpy no longer have this issue)

Sometimes when editing a class with dnSpy, it will duplicate one or more of the class variables after you compile your changes. This can cause many problems, one of which being when you try to make your next edit, you will get an error because of the duplicate existing.

When you scroll down to the list of class variables, and find the one that was duplicated, the version that is further towards the bottom is the duplicate, while the one further up is the original one.

If the method you modified did not make use of any of the variables that got duplicated, it is safe to just delete the duplicate and continue with your edits. However, if any of those duplicates were present in the modified method, then don't delete it yet!! If you delete it, the method you modified will get destroyed, and will become something like this:



And if you save the module you will get an error like this:



If this ever happens, use the Undo function to revert your changes immediately, otherwise your DLL will be permanently broken. To avoid this happening, first note the Token hex number that appears over top the duplicate variable, and over top the original variable. Then go back to the method you modified and find every place where the variable is used.

IL Edit those lines. Each of those places will be pointing to the duplicate, so you want to click on them and change them back to the original (you can tell the two apart based on the token numbers). Once you've changed all of them back to the original, then you can safely delete the duplicate from the class and continue with your editing.

Cannot convert from X to X
(The newest nightly builds of dnSpy no longer have this issue)

This error comes up most often when passing the current class as an argument to another class or function via the "this" keyword. So the lines giving this error will be something like:

this.whatever = new Something(this, x, y, z);

We want to change the "this" keyword temporarily to "null":

this.whatever = new Something(null, x, y, z);

This will stop the errors and will allow you to compile. However, we can't leave them as null! We need to change all of those "null" keywords back to "this". This will be done by going back to each of the lines that we changed to null and IL editing them.

In the IL Edit window, the null keyword will appear as "ldnull", you just need to change that to "ldarg.0", and it should become "this" again.

X does not implement interface member Y
(The newest nightly builds of dnSpy no longer have this issue)

You may encounter this error when editing C# methods on a class that implements an interface. An interface requires a certain set of methods to be present in the class, but when you are editing a method in C#, the class becomes a "partial class" where all the methods other than the one you are editing get hidden. This triggers the error because the partial class no longer conforms to the interface's requirements.

If you look up at the signature for the class/partial class, the interfaces will be the ones after the colon that are colored as gray text. Just remove the interface temporarily (make sure you remember what it is called), and you will be able to compile. However, once you've compiled the method and are done with your edits, you need to add the interface back!

To do this:
 * Right click on the class name.
 * Choose "Edit Type".
 * Go to the "Interfaces" tab.
 * Click "Add..."
 * Click "Type"
 * Search for the name of the interface you deleted, select it, and click "OK".

The type or namespace name X could not be found
This error will appear if you're missing a using directive. Sometimes when editing a method, dnSpy will fail to copy over all the "using" imports that the class actually contains (found at the very top of the file). Just copy all of the using directives from the original class, and paste them into the Edit Code window, so you don't miss any.

Operator is ambiguous on operands of type Vector3 and Vector2
To solve this, convert the Vector2 in the line that's throwing an error into a Vector3. For example:

// This will throw an error v2 + v3;

// This will compile fine new Vector3(v2.x, v2.y, 0) + v3;

Or in any Other Case...
Sometimes there's some error in the method that you just don't know how to get around, or it's just a particularly annoying method to edit because it has a large number of lines that have errors. One good general workaround for many situations is to make a new method with your code changes, then IL edit in a call to the new method into the original method you couldn't edit with C#. This requires just two IL lines:

ldarg.0 call   (reference to your new function)

You can right click in the IL editor to see options for adding new lines/instructions.