Skip to content

Ability to invoke interface method on generic struct without boxing #9026

@stephentoub

Description

@stephentoub

Related to:
dotnet/csharplang#905
https://github.com/dotnet/coreclr/issues/12877

In a method like:

public void Foo<T>(T t)
{
    ...
}

I want to be able to do the equivalent of:

if (t is ISomeInterface)
{
    ((ISomeInterface)t).InterfaceMethod();
}

but without incurring the associated boxing. I can work around https://github.com/dotnet/coreclr/issues/12877 with a hack like dotnet/coreclr@9a06301#diff-bc8ce62cfb625ddcc19d1f21b5c8b5c1R457, but that only addresses the is check; I'm not aware of any way currently to do the invocation without an allocation.

Example where this would be valuable:
dotnet/coreclr@9a06301#diff-bc8ce62cfb625ddcc19d1f21b5c8b5c1R427
Right now for this optimization in async methods, we have no way to special-case all ValueTask<T>s, instead having to special-case just those ValueTask<T>s for Ts we list here explicitly. If we had the ability to do the above pattern, we could make ValueTask<T> implement an internal interface that provided a Task AsTask() method that would let us fish out and use the underlying Task, which enable us to apply the optimization for all ValueTask<T>, regardless of the T.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions