Skip to content
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

Need api to determine if a type is "byref-like." #20673

Closed
ghost opened this issue Mar 17, 2017 · 9 comments
Closed

Need api to determine if a type is "byref-like." #20673

ghost opened this issue Mar 17, 2017 · 9 comments
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection
Milestone

Comments

@ghost
Copy link

ghost commented Mar 17, 2017

Proposal for New API:

   class Type
   {
        public virtual bool IsByRefLike => throw new NotSupportedException();
   }

With Span<>, we're now introducing a mainstream type that has "by-ref-like" or "stack-only" restrictions. We also have TypedReference in this bucket, but Span<> takes this concept out of the "eccentric uncle in the closet" realm.

There is, however, no robust way to know if a type has these restrictions. Apis like Array.CreateInstance() need to guard against these types (dotnet/corert#3000) and could use a formalized way to catch these types. It's likely that checks like these could go viral into app code or higher level libraries that call apis like this.

@karelz
Copy link
Member

karelz commented Mar 17, 2017

Looks somewhat related to recent #19493. Will we need more similar APIs for Span?
cc @jkotas

@jkotas
Copy link
Member

jkotas commented Mar 17, 2017

This should wait for the design for byref-like types (e.g. dotnet/csharplang#264).

I expect that the byref-like types will be explicitly marked by a custom attribute. So this property won't be inferred, but explicitly specified instead and this API will be essentially just a fetch of this custom attribute (or more efficient equivalent).

@MichalStrehovsky
Copy link
Member

This would be an API that should be used in Microsoft.CSharp as well (in isSpecialByRefType). The current implementation of that seems to have been hollowed out when TypedReference and friends weren't in the .NET Core profile. It likely wants the special behavior for Span<T> as well.

Cc @JonHanna

@JonHanna
Copy link
Contributor

@MichalStrehovsky From taking a look at a disassembly of the version in the GAC it seems that that version is implementing isSpecialByRefType by checking against a few specified names, which aren't names the corefx version knows about. Making use of the API proposed here would likely be better than any attempt to just restore the old logic.

@MichalStrehovsky
Copy link
Member

MichalStrehovsky commented Jul 16, 2017

This should wait for the design for byref-like types (e.g. dotnet/csharplang#264).

These now have an approved attribute (System.Runtime.CompilerServices.IsByRefLikeAttribute) that marks them. Maybe we can drop the blocked tag?

[EDIT] Fixed attribute name to IsByRefLikeAttribute by @karelz

@ghost ghost self-assigned this Jul 17, 2017
@terrajobst
Copy link
Member

terrajobst commented Aug 29, 2017

Video

We should align this with the attribute we approved earlier, which is named IsByRefLikeAttribute. Since the proposal matches that, it's approved as proposed.

@karelz
Copy link
Member

karelz commented Aug 29, 2017

FYI: The API review discussion was recorded - see https://youtu.be/VppFZYG-9cA?t=5306 (4 min duration)

ghost referenced this issue in dotnet/corefx Aug 30, 2017
* Expose IsByRefLike api.

Api was approved today:

Fixes https://github.com/dotnet/corefx/issues/17232

CoreRT/CoreCLR implementations were done
some time ago (we needed them internally.)
So this just exposes the api in the contract.

* Also need to override on EnumBuilder and GenericTypeParameterBuilder

* TypeFromCLSID not supported on Unix
@bergmeister
Copy link

bergmeister commented Oct 19, 2018

@terrajobst The IsByRefLike property on System.Type is only available in .Net Core and not in .Net Standard or full .Net. Could I use the IsByRefLikeAttribute instead on full .net as an equivalent implementation of my own for IsByRef or is there another way of using this property on full .net (4.7.2)? The documentation says only This attribute is used by the compiler for tracking metadata. It should not be used by application developers.

@jkotas
Copy link
Member

jkotas commented Nov 1, 2018

@bergmeister Correct, the workaround is to check for presence of IsByRefLikeAttribute. The first class support for byref-like types is present in .NET Core only.

Also see https://github.com/dotnet/corefx/issues/33149

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 25, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Reflection
Projects
None yet
Development

No branches or pull requests

8 participants