IsExternalInit | source code only package which allows you to use C
kandi X-RAY | IsExternalInit Summary
kandi X-RAY | IsExternalInit Summary
:running: Quickstart | :package: NuGet. C# 9 added support for the init and record keywords. When using C# 9 with target frameworks <= .NET 5.0, using these new features is not possible because the compiler is missing the IsExternalInit class, hence making the features unavailable for any target framework prior to .NET 5. Luckily, this problem can be solved by re-declaring the IsExternalInit class as an internal class in your own project. The compiler will use this custom class definition and thus allow you to use both the init keywords and records in any project. This repository hosts the code for the "IsExternalInit" NuGet Package which, when referenced, automatically adds the IsExternalInit class to the referenced project(s). The code for the IsExternalInit class is added at compile time and gets built into the referencing project. This means that the resulting project does not have an explicit dependency on the IsExternalInit package, because the code is not distributed as a standard library. Futhermore, the code only gets added to the project if the IsExternalInit class is not already supported by the target framework. The images below show an example - in the library which targets .NET Standard 2.0 the IsExternalInit class has been added during the compilation. That is not the case for the library targeting .NET 5.0, because it is already present in its BCL. This allows you to easily multi-target your projects without having to change a single line of code.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of IsExternalInit
IsExternalInit Key Features
IsExternalInit Examples and Code Snippets
Community Discussions
Trending Discussions on IsExternalInit
QUESTION
I am unsuccessfully trying to get custom attributes of dotnet record properties through the Reflection API! I have annotated properties with a custom attribute, but as I can see in ILDASM no property, nor field, nor method member has that custom attribute. So, what has the compiler done with that annotation?
Simply try it dotnet new classlib
and then create a DummyAttribute
in the folder such as:
ANSWER
Answered 2021-Mar-25 at 16:30The attribute is applied to the constructor parameter by default. Here's the constructor in the IL:
QUESTION
EDIT [Nov 29 2020]: .NET 5.0 is out now, but the solution below is still required if you're targetting .NET Standard 2.1
C# 9.0 is still under development. There are a couple references which lead me to believe it should be testable now (some of it, anyway).
- A Microsoft blog by Mr. Awesome himself, introducing the features. https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/
- The language tracking page on github: https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md
I'm using VS 2019 16.7 Preview 3.1. I've selected the language version as Preview for a project.
Some C# 9 features, I can use. Like: Dictionary Stuff = new()
But using the new init
feature gives me this error: Error CS0518 Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
How do I fix this?
Examples of code causing the error:
...ANSWER
Answered 2020-Jun-30 at 11:17This is a bug in the current preview and the latest master branch (June 27). A simple record in sharplab.io creates the same error.
Just add the missing type somewhere in your project
QUESTION
I have been having this issues while testing the new features of C# 9 with Visual Studio 2019 Preview. I was testing the init setter, but the compiler shows error with the message:
Error CS0518 Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported. Below is the code snippet that I've tried:
...ANSWER
Answered 2020-Nov-16 at 09:37This is a small bug in Visual Studio 2019 that hasn't been fixed yet. To solve this, you need to add a dummy class named IsExternalInit
with the namespace System.Runtime.CompilerServices
anywhere in your project. That will do it.
If writing a library it's best to make this class internal, as otherwise you can end up with two libraries both defining the same type.
QUESTION
I've created a new Blazor WebAssembly project in Visual Studio which, by default, targets .NET Standard 2.1 and therefore uses C# 8.
I've also written a library written for .NET 5 and used C# 9 in it.
How can I use this library in my Blazor project? I can't reference a .NET 5 library in a .NET Standard 2.1 project.
- When I change the target framework of the library to .NET Standard 2.1 I cannot use C# 9 anymore in the library project. When I explicitly set the LangVersion in the csproj to 9.0 all of my record types get compile errors that some predefined type System.Runtime.CompilerServices.IsExternalInit is not defined
- When I change the target framework of the Blazor project to .NET 5 and reference a .NET 5 library it doesn't work anymore (start up fails with HTTP Error 500.33 - ANCM Request Handler Load Failure)
ANSWER
Answered 2020-Nov-14 at 20:50If you need to migrate your Blazor WebAssembly project to .NET 5, check this out https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-50?view=aspnetcore-5.0&tabs=visual-studio#update-blazor-webassembly-projects
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install IsExternalInit
Reference the package Add the package to your project, for example via: Install-Package IsExternalInit --or-- dotnet add package IsExternalInit
Ensure that the package has been added as a development dependency Open your .csproj file and ensure that the new package reference looks similar to this: <PackageReference Include="IsExternalInit" Version="<YOUR_VERSION>" PrivateAssets="all" /> <!-- NuGet, by default, uses this style. This is also acceptable. --> <PackageReference Include="IsExternalInit" Version="<YOUR_VERSION>"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> This is especially important for libraries that are published to NuGet, because without this, the library will have an explicit dependency on the IsExternalInit package.
Build the project Ensure that the project compiles. If a build error occurs, you will most likely have to update the C# language version.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page