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

    Jun 19, 2023 09:49

    This is such a fascinating and helpful essay; thank you for writing it!

  2. pikcan

    May 08, 2023 03:10

    i like your post

  3. davidwarnnner12

    Mar 15, 2023 14:15

    Thanks

  4. mathersondferwqe

    Jan 11, 2023 03:23

    I love this article, it's very unique and beneficial.

  5. KristinTanner

    Sep 12, 2022 03:59

    great

  6. karenhere

    Apr 04, 2022 10:34

    Visit apnakhata.guide to View Nakal.

  7. RenatoMalota

    Dec 07, 2021 21:16

  8. RenatoMalota

    Dec 07, 2021 21:15

  9. FrederickHenderson

    Aug 24, 2021 10:10

    crickco.com
    broadapk.com

  10. FrederickHenderson

    Aug 24, 2021 10:10

    Good

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.