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

Created on 21 May 2020  路  5Comments  路  Source: reactiveui/ReactiveUI

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.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.

feature request reactiveui-fody

Most helpful comment

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

All 5 comments

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

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

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

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

Thanks

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

Was this page helpful?
0 / 5 - 0 ratings