Skip to content

Should there be a .Net Standard 2.0 version of Reflection.Emit? #26007

Closed

Description

@svick

System.Reflection.Emit is not part of .Net Standard, but there are packages that let you use it from a .Net Standard library (specifically, System.Reflection.Emit and System.Reflection.Emit.Lightweight). But those packages do not have a .Net Standard 2.0 version, only .Net Standard 1.x versions.

This has some implications:

  • .Net Standard 2.0 libraries can't use typeBuilder.CreateType() (even though this code works both on .Net Framework 4.6.1 and .Net Core 2.0) and have to use typeBuilder.CreateTypeInfo().AsType() instead. It's possible there are also other APIs like this.
  • .Net Standard 2.0 libraries that want to use Reflection.Emit still have to use .Net Standard 1.x-style System.* packages.

These issues would be solved if a .Net Standard 2.0 version was added to Reflection.Emit packages. Is that something that would be worth doing?

Though both of these are fairly small quibbles, so I'm not sure how much value would doing this add.

Activity

  1. karelz commented on Apr 27, 2018

    @karelz
    Member

    @ericstj @weshaggard do you know if it is on purpose this way, or an oversight?

  2. ericstj commented on Apr 27, 2018

    @ericstj
    Member

    It was intentional. For libraries like this that have jagged support and no portable implementation we stopped shipping packages. The expectation is that you target a specific framework if you need it. For some we have brought back packages with land mines (throwing implementations on some platforms). Folks could propose we add it back but this one in particular has had some stong debate in the past due to lack of support on all AOT based .net frameworks. /cc @terrajobst

  3. svick commented on Apr 27, 2018

    @svick
    MemberAuthor

    @ericstj

    The expectation is that you target a specific framework if you need it.

    How am I supposed to learn about that? As far as I can tell, Reflection.Emit is not reported by platform-compat. And when I google "reflection emit .Net Standard", the only result on the first page talking about whether you should use it that way is a tweet from July 2017 by @terrajobst suggesting that you should do it (maybe the situation changed since then?).

  4. ericstj commented on Apr 27, 2018

    @ericstj
    Member

    I’ll let @terrajobst respond to that, perhaps he was thinking about the nuget gaurdrails feature we had in 1.x and the 1.x packages. With those we had jagged implementations and build time support for messaging that things aren’t supported. I’m not particularly leaning one way or the other here just explaining the way things are working at the moment. I think if we can get sign off from folks on adding back a package with a platform not supported implementation we could do so.

    FWIW anything that’s available in a framework but isn’t in netstandard or in a library/package with a netstandard asset means you need to target the framework to get it. That’s all I meant with my expectation comment.

  5. svick commented on Apr 27, 2018

    @svick
    MemberAuthor

    @ericstj

    anything that’s available in a framework but isn’t in netstandard or in a library/package with a netstandard asset means you need to target the framework to get it. That’s all I meant with my expectation comment.

    But here the package (with .Net Standard 1.x assets) does exist. I don't think you can just stop updating a package and assume people won't use it anymore. At the very least, you should have documentation explaining the situation.

  6. weshaggard commented on Apr 27, 2018

    @weshaggard
    Member

    The biggest trouble with System.Reflection.Emit is that we don't have a way to provide the library in a netstandard way because of TypeInfo (see https://github.com/dotnet/corefx/issues/14334). The existing package is targeting netstandard1.1 but it is a lie and will only work on .NET Framework and .NET Core because of the tight coupling we have. We hacked it to work by playing InternalsVisibleTo tricks to give it access to TypeInfo's constructors, but that same trick is what makes it not work on a general .NET Standard platform. We decided to not propagate the error further by hacking netstandard2.0 in the same way and instead stopped producing the package and made it platform specific.

    Thanks for raising the issue @svick and we should use this issue to document the trouble with the package.

  7. akoeplinger commented on Apr 27, 2018

    @akoeplinger
    Member

    What about unlisting the no longer updated packages on NuGet so it's clear they are not recommended?

  8. masonwheeler commented on May 7, 2018

    @masonwheeler
    Contributor

    This is very needed. Not being able to build and save assemblies is currently my dotnet/corefx#1 biggest blocker for updating to .NET Core, and I'd really like to see getting this very fundamental API up to speed made a higher priority than working on new features.

  9. weshaggard commented on May 7, 2018

    @weshaggard
    Member

    @masonwheeler just to call out you can use Reflection.Emit if you are writing .NET Core app or library you just cannot use it while writing .NET Standard library currently.

  10. masonwheeler commented on May 7, 2018

    @masonwheeler
    Contributor

    @weshaggard are you sure? I just checked the repo, and it appears that not only is AssemblyBuilder.Save not implemented yet, it doesn't even exist at all on Core! (I might be wrong; I'm on my phone and GitHub's mobile interface isn't the best, but that's what it looks like.)

  11. weshaggard commented on May 7, 2018

    @weshaggard
    Member

    @masonwheeler you're correct that we don't support AssemblyBuilder.Save in .NET Core that is being tracked by another issue https://github.com/dotnet/corefx/issues/4491.

  12. masonwheeler commented on May 7, 2018

    @masonwheeler
    Contributor

    There it is! I thought I had seen that issue before, but searching only turned up this one. :P

    But yeah. Without the ability to actually emit your results, you can't really say that you can use Reflection.Emit on Core. ☹️

  13. SergeyKhutornoy commented on May 10, 2018

    @SergeyKhutornoy

    @weshaggard before the System.Reflection.Emit.Lightweight package being unlisted on nuget.org it was possible to get Selenium.WebDriver package in PowerShell 5 (for .Net Framework) and PowerShell Core 6 (for .Net Core 2.0) on Windows 10 using the following command: Install-Package Selenium.WebDriver -Destination $PSScriptRoot -Force -ForceBootstrap

    Now this command cannot download all the Selenium package dependencies for both PowerShell versions. It fails because System.Reflection.Emit.Lightweight.4.3.0 package is unlisted on nuget.org.
    Error: Install-Package : Unable to find dependent package(s) (System.Reflection.Emit.Lightweight)

    Could you advise on how to fix this issue?

  14. weshaggard commented on May 11, 2018

    @weshaggard
    Member

    @SergeyKhutornoy are you calling "Install-Package" in VS from the Package Manager Console? If so that correctly installs the package for me. If you are calling Install-Package from Powershell that is a different kind of package management. I actually just tried that locally and I get a different error (Install-Package : No match was found for the specified search criteria and package name 'Selenium.WebDriver'.) I'm not familiar with that package management system so I'm not sure how to work around it. One thing you could try is to explicitly install System.Reflection.Emit.Lightweight 4.3.0 first and then see if it works. If that doesn't work is there a reason you cannot use VS or nuget tools to install the package?

  15. 32 remaining items

  16. ericstj commented on Jul 19, 2018

    @ericstj
    Member

    @glassgrass what frameworks do you imagine a netstandard2.0 implementation would apply to?

    I can certainly imagine some implementation that would write out the IL and produce an assembly like an API-driven ILASM, but I don't see current frameworks where I would want to use that.

    Frameworks that have a JIT (and support Assembly.Load) could also support Ref.Emit directly, and provide their Ref.Emit implementation instead of a netstandard2.0 version. I know this is the case at least desktop/.netcore/.netnative. The first two support ref.emit and provide an implementation, the later does not, nor does it have a JIT nor support dynamic assembly loading.

    I do think it would be an interesting project for someone to play around in corefxlab to write something on top of System.Reflection.Metadata that looks like Ref.Emit (or better). Not sure if anyone is looking at that already. /cc @nguerrera @tmat

  17. stakx commented on Jul 19, 2018

    @stakx
    Contributor

    @atsushikan: Glad to see that there is still ongoing work on this issue. Thanks for tackling this.

    I've mentioned the following elsewhere previously: One thing that System.Reflection.Metadata and Assembly.Load(byte[]) cannot do is incremental type-by-type emitting. You can't emit one type, then another... you can only ever emit complete assemblies, which means this is not too useful for e.g. mocking libraries that rely on dynamic proxy type generation.

    Of course one could only ever produce single-type assemblies, but this becomes potentially inefficient & difficult when making internals visible to the dynamically generated assemblies via [assembly: InternalsVisibleTo] (another thing mocking / testing libraries often require). You'd have to be able to give several dynamic single-type assemblies the same identity / strong name for this to work. (The runtime might already allow this in practice, but I can't find documentation officially confirming it.)

  18. tmat commented on Jul 19, 2018

    @tmat
    Member

    @stakx You can emit System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute to the dynamic assembly to allow it to access non-public members of specified assembly, instead of using IVT.

  19. stakx commented on Jul 19, 2018

    @stakx
    Contributor

    @tmat: This is interesting. Is this attribute officially documented somewhere? I can't seem to find any information about it. Never mind, I think I've found enough information about this.

    It's a pity this special attribute isn't officially documented, sort of makes it a bit of a gamble to use it outside the runtime.

  20. abhi18av commented on Nov 3, 2018

    @abhi18av

    Would this be added to .NET core 3.0 ?

  21. karelz commented on Nov 3, 2018

    @karelz
    Member
  22. karelz commented on Nov 8, 2018

    @karelz
    Member

    @joshfree is this one a dupe, or does it cover more?

  23. joshfree commented on Nov 8, 2018

    @joshfree
    Member

    @karelz it covered the issue with the Ref.Emit package being incorrectly delisted from nuget and then the package being re-listed by @weshaggard. I think this issue can now be closed as the remaining work is tracked with dotnet/corefx#30654.

  24. karelz commented on Nov 12, 2018

    @karelz
    Member

    OK, closing then :) ... the remaining work is tracked in dotnet/corefx#30654 as mentioned above.

  25. transferred this issue fromdotnet/corefxon Jan 31, 2020
  26. added this to the 3.0 milestone on Jan 31, 2020
  27. ghost locked as resolved and limited conversation to collaborators on Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflection.Emitquestion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions