How to Resolve Error "CS0730: Cannot forward type 'type' because it is a nested type of 'type'" in C#
The Compiler Error CS0730 is a specific restriction related to Type Forwarding. The message reads: "Cannot forward type 'Outer.Inner' because it is a nested type of 'Outer'".
Type Forwarding ([TypeForwardedTo]) allows you to move a class from one assembly (DLL) to another without breaking existing applications that reference the old assembly. However, the Common Language Runtime (CLR) only supports forwarding top-level types (classes defined directly inside a namespace). You cannot forward a nested type (a class defined inside another class) individually. Nested types are intrinsically linked to their containing class and cannot be redirected independently.
This guide explains how to handle type forwarding when nested classes are involved.
Understanding Type Forwarding Rules
Type forwarding is used during library refactoring.
- Old Assembly: Contains
[assembly: TypeForwardedTo(typeof(MyClass))]. - New Assembly: Contains the actual code for
MyClass.
The restriction is that MyClass must be a root-level citizen of the namespace (e.g., MyNamespace.MyClass). It cannot be MyNamespace.Container.MyClass.
- Logical Reason: A nested type exists within the scope of its container. You cannot say "The Container is here, but the Inner part is in that other DLL." The container owns the definition of its nested members.
Scenario: Attempting to Forward a Nested Class
This error often occurs when a developer moves an entire class structure (Parent and Child classes) to a new library and tries to be "thorough" by adding a TypeForwardedTo attribute for every single class, including the private or public nested ones.
Example of error:
using System.Runtime.CompilerServices;
public class Outer
{
public class Inner { }
}
// ⛔️ Error CS0730: Cannot forward type 'Outer.Inner' because it is a nested type.
// You are allowed to forward 'Outer', but not specifically 'Outer.Inner'.
[assembly: TypeForwardedTo(typeof(Outer.Inner))]
Solution 1: Forward the Outer Class
If you moved the Outer class (and its Inner class inside it) to a new assembly, you only need to forward the Outer class.
When the runtime resolves Outer to the new assembly, it naturally finds Inner inside it. You do not need (and cannot use) a separate forwarder for the nested type.
Solution: remove the attribute for the inner type and ensure the outer type is forwarded.
using System.Runtime.CompilerServices;
// ✅ Correct: Forwarding the parent automatically handles the nested types
// contained within that parent in the destination assembly.
[assembly: TypeForwardedTo(typeof(Outer))]
// (The actual code for Outer and Inner should be deleted from this project
// and exist in the referenced 'NewLibrary' project).
Solution 2: Un-nest the Type (Breaking Change)
If your goal was to move only the inner class to a new library while keeping the outer class in the old library, this is impossible while maintaining the nested relationship.
To support this, you must refactor the class to be a top-level class. Note that this changes the class name from Outer.Inner to MyNamespace.Inner, which is a breaking change (existing code referencing Outer.Inner will fail).
The Refactoring:
- New Assembly: Define
Inneras a top-level class. - Old Assembly: Forward the now top-level type.
using System.Runtime.CompilerServices;
// ✅ Correct: Now 'Inner' is a top-level class, it can be forwarded.
// BUT: You must have changed the class definition to be non-nested.
[assembly: TypeForwardedTo(typeof(Inner))]
Conclusion
CS0730 reminds you that nested types do not exist independently of their container.
- Check Hierarchy: Is the type you are forwarding inside another
class? - Target the Parent: Forward the Outer Class. This implicitly redirects users to the new assembly where they will find the inner class.
- Clean Up: Remove any explicit
TypeForwardedToattributes pointing to nested types.