Επεξεργασία

Κοινή χρήση μέσω


IL2026: Members attributed with RequiresUnreferencedCode may break when trimming

Cause

Calling (or accessing via reflection) a member annotated with RequiresUnreferencedCodeAttribute.

For example:

[RequiresUnreferencedCode("Use 'MethodFriendlyToTrimming' instead", Url="http://help/unreferencedcode")]
void MethodWithUnreferencedCodeUsage()
{
}

void TestMethod()
{
    // IL2026: Using method 'MethodWithUnreferencedCodeUsage' which has 'RequiresUnreferencedCodeAttribute'
    // can break functionality when trimming application code. Use 'MethodFriendlyToTrimming' instead. http://help/unreferencedcode
    MethodWithUnreferencedCodeUsage();
}

Rule description

RequiresUnreferencedCodeAttribute indicates that the member references code that may be removed by the trimmer.

Common examples include:

  • Load(String) is marked as RequiresUnreferencedCode because the Assembly being loaded may access members that have been trimmed away. The trimmer removes all members from the framework except the ones directly used by the application, so it is likely that loading new Assemblies at run time will try to access missing members.

  • XmlSerializer is marked as RequiresUnreferencedCode because XmlSerializer uses complex reflection to scan input types. The reflection cannot be tracked by the trimmer, so members transitively used by the input types may be trimmed away.