Search Issue Tracker

By Design

Votes

0

Found in

2018.4

2018.4.23f1

2020.2

Issue ID

1256394

Regression

No

DisplayAttribute of a property using Reflection the type of Attribute is incorrect when using System.ComponentModel.Annotations

Mono

-

How to reproduce:
1. Open the attached 'DataAnnotations.zip' project
2. Open 'SampleScene' Scene
3. Enter Play Mode
4. Observe the last Console log which attempts to get the attribute using Reflection from an externally provided assembly

Expected result: 'DisplayAttribute: System.ComponentModel.Annotations, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' logged to the Console
Actual result: 'DisplayAttribute: System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' logged to the Console

Reproducible with: 2018.4.24f1, 2019.4.2f1, 2020.1.0b12, 2020.2.0a15

Notes:
- This only happens for libraries that target .NET Standard 2.0, are compiled externally from Unity, and imported as a managed plugin. If the project targets .NET 4.x then it works appropriately.

  1. Resolution Note:

    Unity does not support assemblies from NuGet that are built against .NET Standard 2.0 but replace behavior from .NET Framework 4.7.1. It looks like this is the case for the System.ComponentModel.Annotations.dll assembly in the project.

    It provides the implementation of System.ComponentModel.DataAnnotations.DisplayAttribute as part of the ".NET Standard + Platform Extensions" platform (see https://apisof.net/catalog/System.ComponentModel.DataAnnotations.DisplayAttribute for details).

    This means that System.ComponentModel.DataAnnotations.DisplayAttribute is not in .NET Standard 2.0. Unity does provide an implementation of it with .NET 4.7.1. The problem occurs when the System.ComponentModel.Annotations.dll is used in the project - then Unity has two implementations of DisplayAttribute, and the it is unsure which one to use.

    If you would like to use DisplayAttribute in a project, the proper approach is to

    1. Set the "Api Compatibility Level" option in the Player Settings to be .NET 4.x.

    2. Add a csc.rsp file to the Assets folder of the project with these contents: -reference:System.ComponentModel.DataAnnotations.dll

    3. Delete the System.ComponentModel.Annotations.dll file from the project.

Comments (11)

  1. DejanKulusevski

    Jul 09, 2021 12:35

Add comment

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.