-
Notifications
You must be signed in to change notification settings - Fork 8
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
REFL013 warns about using events when it's not possible to do so #206
Comments
I think it's telling you to use .GetEvent("OnModel", ...).Add.Invoke(...) which wouldn't make sense here. Your OnModel member is a normal public field, not an event, right? |
OnModel is a using System;
public delegate void OnModelHandler(object e);
public interface IThing
{
event OnModelHandler OnModel;
}
public class TestClass : IThing
{
public event OnModelHandler OnModel;
} |
Ah, so you're using a field-like event. There is no guarantee that you'll be able to access the backing field of an event by that name in general. For example: public class TestClass : IThing
{
public event OnModelHandler OnModel
{
add => otherThing.OnModel += value;
remove => otherThing.OnModel -= value;
}
} You are relying on a compiler implementation detail. All you know for sure if you have an event is that there is an @JohanLarsson It appears that REFL013 is triggering for GetField on event metadata. REFL013 should only trigger for Sharplab shows that in this scenario there are two IL members named
|
in my scenario, it's just for unit testing purposes, thankfully. |
@SirJosh3917 I just noticed that you have |
Maybe a bug? The analyzer does not realize that a private field is emitted as a current Roslyn implementation detail: class C
{
public event EventHandler E;
static EventHandler GetDelegateFromPrivateBackingField(C instance)
{
// If you run this code, it will work as intended until a Roslyn impl detail changes.
return (EventHandler)typeof(C)
.GetField(
nameof(E),
// REFL005 There is no member matching the filter. Expected: BindingFlags.Public |
// BindingFlags.Instance | BindingFlags.DeclaredOnly.
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.GetValue(instance);
}
} On the other hand, Roslyn could decide to change the naming scheme of the private backing field any day as far as I know. I'd still say that the analyzer should not make incorrect definitive statements. There is a member matching the filter when the code compiles. |
Now, back to the repro originally reported. I think that some warning must actually be shown because of the missing class C
{
public event EventHandler E;
static EventHandler GetDelegateFromPrivateBackingField(C instance)
{
// If you run this code, it will NRE due to missing BindingFlags.NonPublic.
return (EventHandler)typeof(C)
// ↓ REFL013 The type C has a event named E.
.GetField(
nameof(E),
BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.GetValue(instance);
}
} |
REFL005 should handle that I think. |
Problem is a bug in Roslyn: dotnet/roslyn#36259 |
reflection code:
the (what i assume to be suggested) code:
The text was updated successfully, but these errors were encountered: