Giter Site home page Giter Site logo

manuelroemer / isexternalinit Goto Github PK

View Code? Open in Web Editor NEW
97.0 97.0 4.0 89 KB

A source code only package which allows you to use C# 9's init and record features in older target frameworks like .NET Standard 2.0 or the "old" .NET Framework by providing a polyfill for the IsExternalInit class.

Home Page: https://www.nuget.org/packages/IsExternalInit

License: MIT License

C# 100.00%
compiletime init isexternalinit polyfill record source

isexternalinit's Introduction

isexternalinit's People

Contributors

arahaan avatar gtbuchanan avatar manuelroemer avatar skeller1 avatar sweini avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

isexternalinit's Issues

Use in .Net Framework 3.5

I'm trying to use this in a .Net Framework 3.5 project with the language version set to C#9, like so:

public record TestRecord
{
    public string Name { get; init; }
}

I am getting the following error:

[CS0656] Missing compiler required member 'System.Type.op_Equality'

Is it even possible to use C# 9 records in .Net Framework 3.5?

How about adding Range+Index to allow range syntax too?

Hey there,
I love your Nullable and IsExternalInit polyfills. I noticed there's another (slightly more involved) one that would be awesome to combine with those two: support for range syntax on NS2 (probably others too?).

The code needed to support that is basically https://github.com/devlooped/avatar/blob/main/src/Avatar.UnitTests/Range.cs (which I copy-pasted from corefx, since that was an earlier, simpler version), which you can see working on that net472 multi-targeting project. I use that same file in other NS2 projects too.

I'm just too lazy to clone the entire approach you already have, so I thought I might just suggest you do it instead, he :).

And FWIW, I think a meta-package that brought them all would be nice too :).

IsExternalInit must be declared for all TFMs

Unlike other polyfill packages where you can get away with declaring attributes only where the TFM does not declare them, IsExternalInit must be declared in the same assembly forever and for all TFMs or else it's a binary breaking change.

I see in your nuspec that you omit the declaration for net5 but you can't do that. You must declare it there as well.
The reason being the compiler generates metadata on the init property accessors that references this attribute using its assembly-qualified type name. When other assemblies reference this one and invoke an init accessor, the compiler embeds that exact same assembly-qualified reference into their invocation of that accessor. Now imagine this referencing library runs in a process where the referenced library is no longer the one that targeted net472 but targets net5. The CLR will throw a MissingMethodException when it encounters the init accessor because the attribute on the accessor does not match the IsExternalInit reference in the caller.

See proof of the breaking change

Why is this needed?

I don't understand.
I have a netstandard2.0 assembly using LangVersion 9.0 and with this added:
namespace System.Runtime.CompilerServices
{
internal class IsExternalInit { }
}
Then init works.

I can reference this assembly from net5.0 assemblies (using default LangVersion 9.0) and from .NET framework 4.7.2 using default LangVersion (assume 7.3). So why would I need this?

Duplicate definition error in VS 2022

I'm trying to upgrade my solution to VS 2022, but if I build it I'm getting duplicate definition errors that weren't there before with VS 2019. I guess this is caused by the new .net 6 sdk?

I think a simple future proof fix would be to wrap the source code in another #if block

#if !NET
...
#endif

since NET is defined for .net 5 and later.

Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported

When using the package I get this error with my records. (net461/netstandard2.0/netstandard2.1)

But additonally using

namespace System.Runtime.CompilerServices
{
    internal class IsExternalInit { }
}

will show that this is already included by the package.

When I build the solution by starting a project within the solution that references the project using IsExternalInit, it compiles. But when I compile the standalone project it shows this error for each record.

It used to work not too long ago, but now it doesn't, very strange. I'm using VS 17.2 and 17.3 Preview.

Only using my own IsExternalInit works every time, but I would like to use the package...

Rebuild of a solution targeting multiple frameworks fails on Visual Studio 17.2.0 & 17.2.1

It might be Microsoft's fault but on Visual Studio 17.2.0 & 17.2.1 a build of a solution targeting multiple frameworks and containing your package works fine, whereas a rebuild always fails with "Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported".

It can easily be reproduced with your test project PackageReference.MultiTarget.
Clean & Build works fine.
Rebuild fails.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.