Open sandboxFocusImprove this doc

LA0202: Method with an In Parameter Cannot Be Introduced into an Older Target Framework

Severity Error
Reported by PostSharp
Message Cannot introduce a method with an in parameter into type "<TargetType>" because System.Runtime.CompilerServices.IsReadOnlyAttribute is not available in its assembly. Target a framework that defines IsReadOnlyAttribute, or remove the in modifier from the parameter.

Cause

The C# compiler marks an in parameter with the System.Runtime.CompilerServices.IsReadOnlyAttribute attribute. When the core library of the target framework does not define this attribute, the compiler generates an internal copy of it in each assembly that needs it.

When an aspect introduces a method with an in parameter with IntroduceMemberAttribute into a type of another assembly, PostSharp copies the attributes of the parameter onto the introduced method. An internal IsReadOnlyAttribute of the assembly of the aspect cannot be used in the target assembly, so PostSharp looks for an IsReadOnlyAttribute that the target assembly can use: first in the target assembly itself, then in the core library of its target framework. PostSharp reports LA0202 on the target type <TargetType> when it finds neither.

This message depends on a target framework whose core library does not define IsReadOnlyAttribute, which the test projects of this documentation do not target, so no example can show it.

Resolution

  • Target a framework whose core library defines IsReadOnlyAttribute, for example .NET Core 2.1 or later, or .NET Standard 2.1. The C# compiler then uses the public attribute, and PostSharp copies it without error.
  • Declare an in parameter in a method of the target assembly. The C# compiler then generates IsReadOnlyAttribute in the target assembly, and PostSharp uses this one.
  • Remove the in modifier from the parameter of the introduced method, so that the parameter is passed by value.

See Also

Reference

IntroduceMemberAttribute

Other Resources

Introducing Interfaces, Methods, Properties and Events into Existing Classes