-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider annotations on the list of types (?) #110239
Comments
Tagging subscribers to this area: @dotnet/illink |
This would require annotating not just list of the types, but also the enumerator and IEnumerable. If you want a workaround, we've been using patterns like this: struct ListOfActivatableTypes
{
private readonly List<Type> _activatableTypes = new List<Type>();
public ListOfActivatableTypes() { }
public void Add([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
=> _activatableTypes.Add(type);
public Enumerator GetEnumerator() => new Enumerator(this);
public struct Enumerator : IEnumerator<Type>
{
private List<Type>.Enumerator _enumerator;
public Enumerator(ListOfActivatableTypes list) => _enumerator = list._activatableTypes.GetEnumerator();
public bool MoveNext() => _enumerator.MoveNext();
public void Reset() => ((IEnumerator)_enumerator).Reset();
public void Dispose() => _enumerator.Dispose();
[UnconditionalSuppressMessage("", "IL2073", Justification = "We ensure only annotated things are added")]
[UnconditionalSuppressMessage("", "IL2093", Justification = "The annotation is on the return type and shouldn't matter")]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type Current => _enumerator.Current;
object IEnumerator.Current => Current;
}
} It requires warning suppressions, but the invariants are relatively easy to ensure since it's pretty compact. |
Thank you. That's nice workaround, but this is business orienter project, so I don't know can it fly or not. Anyway, thanks for your help, I think it's really nice way to move forward, since that's internal property. |
Another solution which we've also used it to wrap the struct ActivatableType
{
public ActivatableType([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
{
this.type = type;
}
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
public Type type;
}
List<ActivatableType> InterceptorProviders { get; } = [new ActivatableType(typeof(string))];
void TestIt()
{
foreach (var i in InterceptorProviders)
{
Activator.CreateInstance(i.type); // No warning
i.type.GetMethods(); // Warns
}
} The usage is a bit worse, but it doesn't require any suppressions and you can use any collection type with it. |
Consider this configuration
And the code whcih use this configuration
There no way how I can say that List of Types will contain only types with specific DAM properties. I think having that would still be statically analyzeable.
The text was updated successfully, but these errors were encountered: