ReactiveUI icon indicating copy to clipboard operation
ReactiveUI copied to clipboard

[Feature Request] Fody does not support property initializers (C# 6+) on generic classes

Open bawkee opened this issue 4 years ago • 8 comments

This is an odd one but is extremely easy to reproduce:

  1. Create a new project, i.e. a console app.
  2. Create a generic class derived from ReactiveObject.
  3. Add a property with an auto-property initializer (a feature of C# 6) to the class.
  4. Decorate the property with the [Reactive] attribute.
  5. Construct the class and run the code.

Here's the code:

// This class throws an exception
public class GenericFoo<T> : ReactiveObject
{
	[Reactive] public object FooObj { get; set; } = new object();
}

// This class works fine
public class Foo : ReactiveObject
{
	[Reactive] public object FooObj { get; set; } = new object();
}

You can construct the Foo class but when ctoring the GenericFoo you will get an exception. For example, running the code:

var f2 = new GenericFoo<object>();

Throws the following runtime exception:

System.MissingFieldException: 'Field not found: 'FodyBug.GenericFoo`1.<FooObj>k__BackingField'.'

Workaround: The workaround is not to use auto-property initializer and just initialize the property in ctor. This only happens in generic classes. This took me a few hours to figure out since I started with a regular class, made it generic and was suddenly getting the strange exception.

The .Net Core 3.1 console project to reproduce the issue: FodyBug.zip

Tested on: Windows 10 x64, Visual Studio 2019 v16.5.5, RxUI/Fody v11.4.1, .Net Core 3.1

Personal note It's kind of a strange notion, to use these initializers on Fody-ed properties in the first place, but it does work on non-generic classes which is pretty awesome actually. Purely based on that I'm guessing it can work just the same on generic classes which is why I labeled this as a bug.

bawkee avatar May 21 '20 17:05 bawkee

Same behavior on my side. Very strange error message 'Common Language Runtime detected an invalid program'.

[Reactive] public decimal dummy{get;set;} =0m;

Hard to find, easy to reproduce.

Tested on: Windows 10 x64, Visual Studio 2019 v16.6.0, RxUI/Fody v11.4.1, .Net 4.8, Desktop

TSSoftware avatar May 24 '20 20:05 TSSoftware

Btw. it is not limited to RxUI/Fody v11.4.1 I stopped testing downwards on v.11.3.1

TSSoftware avatar May 24 '20 20:05 TSSoftware

This is currently being worked on and is fixed in a rewrite of the fody btw.

glennawatson avatar Aug 31 '20 03:08 glennawatson

Hello, "rewrite of the fody"? You mean like upcoming versions?

Thanks

tomasfil avatar Oct 29 '20 13:10 tomasfil

Yes. We are working to fix some of the internals of Fody. It should be coming in a near future release.

RLittlesII avatar Oct 29 '20 15:10 RLittlesII

Is there a tracking issue on the progress over at Fody?

kroimon avatar Oct 06 '21 15:10 kroimon

The fody is going into maintenance-only mode at the moment while we work on a new project which will replace the functionality in general. Keeping this open to track for the new project.

glennawatson avatar Nov 02 '21 04:11 glennawatson

The fody is going into maintenance-only mode at the moment while we work on a new project which will replace the functionality in general. Keeping this open to track for the new project.

Hi, what is the new project called?

StepanMynarik avatar Jun 18 '22 19:06 StepanMynarik

Any updates on the issue? Still present in the newest versions.

Metadorius avatar Oct 26 '22 11:10 Metadorius

@Metadorius See https://github.com/reactiveui/ReactiveUI/issues/2416#issuecomment-957094991

glennawatson avatar Oct 26 '22 11:10 glennawatson

@glennawatson I have seen that, and I asked with that in mind. There were no news on that new project in this issue which you said you will leave open to track and Fody is still being used, so you may treat my question as "what happened to that initiative". Thanks.

Metadorius avatar Oct 26 '22 11:10 Metadorius

https://github.com/reactiveui/ReactiveObject.Generators Is a repo for the new generators. There's been some offers from the community to start coding but hasn't been started in. I've had too much real life lately to get started and hoping it'll clear up soon.

GitHub
Contribute to reactiveui/ReactiveObject.Generators development by creating an account on GitHub.

glennawatson avatar Oct 26 '22 11:10 glennawatson

Thanks much for the response (and for the project(s) in general) and good luck with IRL stuff :)

Metadorius avatar Oct 26 '22 11:10 Metadorius

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Nov 10 '22 00:11 github-actions[bot]