IL2076: 'target generic parameter' generic argument does not satisfy 'DynamicallyAccessedMembersAttribute' in 'target method or type'. The return value of method 'source method' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to

Cause

The target location declares some requirements on the type value via its DynamicallyAccessedMembersAttribute. Those requirements must be met by those declared on the source value also via the DynamicallyAccessedMembersAttribute. The source value can declare more requirements than the target, if necessary.

Example

public void GenericWithAnnotation<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] T>()
{
}

Type GetType() => typeof(int);

void TestMethod()
{
    // IL2076 Trim Analysis: AnnotatedGenerics.TestMethod(Type): 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in 'GenericWithAnnotation<T>()'. The return value of method 'GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to
    typeof(AnnotatedGenerics).GetMethod(nameof(GenericWithAnnotation)).MakeGenericMethod(GetType());
}

Fixing

See Fixing Warnings for guidance.