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

Getting a ref return value using PropertyInfo #24520

Closed
JamesNK opened this issue Dec 26, 2017 · 6 comments
Closed

Getting a ref return value using PropertyInfo #24520

JamesNK opened this issue Dec 26, 2017 · 6 comments

Comments

@JamesNK
Copy link
Member

JamesNK commented Dec 26, 2017

    public class RefAndRefReadonlyTestClass
    {
        private int _refField;
        private readonly int _refReadonlyField;

        public RefAndRefReadonlyTestClass(int refReadonlyField)
        {
            _refReadonlyField = refReadonlyField;
        }

        public ref int RefField => ref _refField;

        public ref readonly int RefReadonlyField => ref _refReadonlyField;

        public void SetRefField(int value)
        {
            _refField = value;
        }
    }

    [Test]
    public void GetRefReturnProperty()
    {
        PropertyInfo property = TestReflectionUtils.GetProperty(typeof(RefAndRefReadonlyTestClass), nameof(RefAndRefReadonlyTestClass.RefField));

        RefAndRefReadonlyTestClass c1 = new RefAndRefReadonlyTestClass(123);
        c1.SetRefField(456);

        object v = property.GetValue(c1, null);
    }

The code above throws this exception from PropertyInfo.GetValue: System.NotSupportedException: 'ByRef return value not supported in reflection invocation.'

Can you use a PropertyInfo to get the value of a ref return property?

[EDIT] Add C# syntax highlight by @karelz

@benaadams
Copy link
Member

Perhaps related?

Bring back TypedReference https://github.com/dotnet/corefx/issues/14088 (Resolved and part of .NET Standard 2.0)
"Proposal for high-performance codegen-less Reflection factory APIs" https://github.com/dotnet/corefx/issues/24390

Though I assume is a current issue for serializing to and from ref returns?

@benaadams
Copy link
Member

benaadams commented Dec 26, 2017

Currently it looks like is definately going to ThrowNoInvokeException

image

Ref returns themselves are an issue with boxing; however...

I think it makes sense that PropertyInfo.GetValue and PropertyInfo.SetValue would operate on the referenced value rather than the reference itself; behaving like FieldInfo.GetValue and FieldInfo.SetValue

And/Or... like FieldInfo should be methods?

public partial class PropertyInfo
{
    public virtual object GetValueDirect(TypedReference obj)
    public virtual void SetValueDirect(TypedReference obj, object value)
}

/cc @VSadov @jkotas

@jkotas
Copy link
Member

jkotas commented Dec 26, 2017

cc @atsushikan @KrzysztofCwalina @ahsonkhan Span<T> end-to-end gap.

@JamesNK
Copy link
Member Author

JamesNK commented Dec 26, 2017

There is a difference between getting a ref return by value and by ref, right? (I haven't used ref returns in anger yet so I'm not completely familiar with it)

e.g.

    RefAndRefReadonlyTestClass c1 = new RefAndRefReadonlyTestClass(123);
    c1.SetRefField(456);

    int byValue = c1.RefField;
    ref int byRef = ref c1.RefField;

I would expect PropertyInfo.GetValue to do the first instead of throwing. I would expect there be some way to do the second with PropertyInfo as well. Not every platform has IL generation.

@joshfree joshfree assigned ghost Feb 6, 2018
@ghost
Copy link

ghost commented Apr 9, 2018

This is a subset of https://github.com/dotnet/corefx/issues/15960 (15960 covers all existing Invoke methods, not just PropertyInfo.GetValue. So I'm closing this as a dupe of that.

@ghost ghost closed this as completed Apr 9, 2018
@caterpillar-toughb
Copy link

Sorry to trouble you folks, but is this working in some form in .NET now?
Looking at this dotnet/coreclr#17639 it would seem to suggest that it's possible.

@caterpillar-toughb caterpillar-toughb unassigned ghost Dec 4, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 19, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants