Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Mono Upgrade Modern .NET Class Library Profiles

Discussion in 'Experimental Scripting Previews' started by joncham, Sep 8, 2017.

?

What modern .NET class library profiles do we need?

  1. .NET Framework Full, .NET Framework Subset, and .NET Standard

    55 vote(s)
    32.0%
  2. .NET Framework Subset and .NET Standard

    98 vote(s)
    57.0%
  3. I don't understand all this profile stuff

    19 vote(s)
    11.0%
  1. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    Yes, that is fine and well, it will allow current projects to move on. But when you say .NET Standard 2.0 is the new default, I think a lot of new projects will be using that. I'd rather not continue to put focus into a "legacy" target if I can avoid it. Performance wise using reflection and emit at build time and generating assemblies for my project will be superior and would work with both Compabitility Levels (Win!), I just need a way past this annoying snag.
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Note that .NET Standard 2.0 is not the same as .NET 2.0. The naming here is very confusing.

    .NET Standard 2.0 is not a legacy target, but rather is the latest cross-platform API profile from Microsoft.

    Note that Microsoft is currently working on defining .NET Standard 2.1, which will have System.Reflection.Emit (among other additions). We plan to support .NET Standard 2.1 in Unity when it is complete.
     
    Qbit86 likes this.
  3. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    I'm well aware that .NET Standard is the new thing, I meant that there seems to be an increased focus on AOT platforms. Will .NET Standard 2.1 have System.Reflection.Emit on AOT platforms too? If not I'll consider Reflection.Emit a nice tool to build custom assemblies both for auto generated code but also for doing things my regular C# isn't allowed to do... like access private fields efficiently. FieldInfo.GetValue can access the fields, it's just a suboptimal path with other limitations like not accessing unsafe pointers properly.
     
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    No, it will not. AOT platform restrictions prevent any implementation from generating executable code at runtime. It is possible to "simulate" System.Reflection.Emit with an interpreter on AOT platforms, but the performance won't likely be sufficient.
     
  5. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    @JoshPeterson thanks for the clarification, it is as I suspected.

    Can you shed some light on how to make a dll obtain the full trust necessary to access private fields of another dll within my own unity project? Obviously I could bypass it through unsafe but only if I make assumptions about the .net runtime which I'd rather not do.
     
  6. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    You could use reflection, but I'm not familiar with any other means. What do you mean specifically by "obtain the full trust"?

    Also, I'd recommend you do not access private fields of Unity assemblies. Those are subject to change at any time, and accessing them will make upgrading Unity versions more difficult.
     
  7. UnLogick

    UnLogick

    Joined:
    Jun 11, 2011
    Posts:
    1,745
    As I was researching this I stumbled upon several references to this being possible under the right circumstances, some said gac, some mentioned signing, others trust, they were all very vague. Trying to go back and find these I've found several posts from microsoft employees saying not possible at all, so perhaps I was chasing a rumor to begin with.

    For all intents and purposes I could use Reflection.GetValue for most things, it's slower but would do the trick, except when trying to access fields of unsafe types like char*. Any ideas on how to make a char* survive the cast to object in GetValue because all I seem to be getting is the first char as a value.
     
  8. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    I'm pretty unfamiliar with anything like this, so I can't offer more help, sorry!
     
  9. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Note that Unity is used in a lot of contexts where build size absolutely doesn't matter - internal company apps, interactive exhibits, ... especially in a brand communication context. A lot of times, this requires the use of obscure and uncommon external libraries that more often than not rely on .NET Full instead of .NET Standard.

    Please don't remove the "I want everything" option, even if you only keep it for Desktop platforms.
     
    r618 likes this.
  10. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    We do not plan to remove the "full" .NET Framework profile option.
     
    fherbst and r618 like this.
  11. fherbst

    fherbst

    Joined:
    Jun 24, 2012
    Posts:
    802
    Thanks for the clarification. I couldn't find the point where that was decided throughout this thread and was a bit frightened by your start post which still says as goal ".NET Framework (Full) - Deprecate/Remove".
    - Might make sense to clarify at the bottom of that first thread what the current thinking is.
     
  12. R1PFake

    R1PFake

    Joined:
    Aug 7, 2015
    Posts:
    540
    I don't know if this is the correct thread to ask this question, but im a little confused with the current Unity Mono/.NET state. So my first question is there any way to "avoid" Mono by using .NET core/standard will Unity always use Mono no matter which .NET im using?

    Also how can I confirm/check that the Unity Mono implementation behaves the same as the "normal" .NET implementation?

    This might sound like a weird question, but I have a very specific case The the System.Net.Sockets have a SendAsync method, in the "normal" .NET implementation the Completed event is not called until all bytes are sent (or a error happens). In some Mono implementations this isn't true and the Completed event can (only in some cases) be called before all bytes were sent (in this case you have to check how many bytes were sent and then call SendAsync again with the remaining bytes). So it would be a problem if I don't check the "TransferedBytes" property, which isn't needed in the "normal" .NET and then suddendly the Unity Mono version has a different implementation and my networking code breaks on some targets.

    And maybe there are other cases where the Mono implementation is different in some special cases, so what's the best way to deal with these situations? The SendAsync method isn't even documented how it should behave, there is no official statement if the Completed event should only be called if all bytes are sent or if it should already be called when some bytes are sent, which makes it an even more special case. (But it would make more sense if it is only called after all bytes are sent, like in the "normal" .NET implementation)

    (Even more off-topic: This networking code is in a own dll which I also use for my other C# projects like Windows desktop stuff, all of them use the "normal" .NET, so I could add the check to the code in this case, but it feels a little dirty to add "Unity/Mono specific" checks just because their Mono imlementation might do something different)
    https://docs.microsoft.com/en-us/do...ockets.socket.sendasync?view=netframework-4.8
     
    Last edited: Apr 21, 2019
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    Let me first mention naming, as the .NET ecosystem has a number of different naming schemes and version numbers, which can be confusing. .NET Core is a class library implementation that runs on the CoreCLR runtime. .NET Standard is a class library API surface that many different class library implementations implement.

    Unity does not support the .NET Core class library implementation at all now. Unity does support .NET Standard 2.0. That means any .NET assembly (i.e. .dll file) built against .NET Core will not work with Unity, but one built against .NET Standard 2.0 will work with Unity.

    Unity has the concept of "scripting backend", which indicates the .NET runtime the Unity player will use for a given platform. For desktop platforms (Windows, Linux, macOS) and Android, you have the choice to use Mono or IL2CPP as the scripting backend. Other platforms must use IL2CPP as the scripting backend.

    Note that both Mono and IL2CPP share the class library implementation code, but they do have different runtime implementations. So it is possible to avoid using Mono as a runtime, since you can select the IL2CPP scripting backend. However even in that case, you will be using some of the same class library implementation code as Mono.

    You will need to test the specific case of interest to you. We do a good bit of testing to confirm that Mono works correctly with Unity, but there may be corner cases we miss. It sounds like the behavior of this SendAsync method is one such corner case. If you do find that Mono in Unity behaves differently than .NET Framework or .NET Core, please submit a bug report to Unity, and we will investigate it.
     
    R1PFake and Thaina like this.
  14. MechEthan

    MechEthan

    Joined:
    Mar 23, 2016
    Posts:
    166
    Is your github fork still a good place to dig-in on understanding Unity's implementation-specific behaviors for .NET Apis?

    e.g. https://github.com/Unity-Technologies/mono/tree/unity-2019.1-mbe for 2019's ".NET 4.x Equivalent" Scripting Runtime Version

    ... and digging in here for the SendAsync question?
    https://github.com/Unity-Technologi...ass/System/System.Net.Sockets/Socket.cs#L1859
     
  15. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    This is a good point, I should have mentioned it! Yes, this is the branch for Unity 2019.1, and that looks like the proper code to inspect.
     
    MechEthan likes this.
  16. Shpendicus

    Shpendicus

    Joined:
    Jan 12, 2018
    Posts:
    5
    I would love to know, why in 2020 august, mono 5.x version and .net 4.7, there is still no support for 3/4 of interopService functions like: Marshal.getActiveObject(string progID)`?? or type.getTypeFromprogID(..)

    Is this anyhow still archivable??

    any answer on this would be nice
     
  17. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,931
    For the most part, Unity is not supporting COM, so we've not prioritized support for these methods.
     
    Qbit86 likes this.