-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
samples/HE/NET6.0/WebapiWIthController/Controllers/WeatherForecastController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace WebapiWIthController.Controllers | ||
{ | ||
[ApiController] | ||
[Route("[controller]")] | ||
public class WeatherForecastController : ControllerBase | ||
{ | ||
private static readonly string[] Summaries = new[] | ||
{ | ||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" | ||
}; | ||
|
||
private readonly ILogger<WeatherForecastController> _logger; | ||
|
||
public WeatherForecastController(ILogger<WeatherForecastController> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
[HttpGet(Name = "GetWeatherForecast")] | ||
public IEnumerable<WeatherForecast> Get() | ||
{ | ||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast | ||
{ | ||
Date = DateTime.Now.AddDays(index), | ||
TemperatureC = Random.Shared.Next(-20, 55), | ||
Summary = Summaries[Random.Shared.Next(Summaries.Length)] | ||
}) | ||
.ToArray(); | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding.Metadata; | ||
|
||
namespace WebapiWIthController | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>(); | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Add services to the container. | ||
|
||
builder.Services.AddControllers(); | ||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | ||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
var app = builder.Build(); | ||
|
||
|
||
var action = @" | ||
var modelMetadataProvider = arg1.Services.GetService<IModelMetadataProvider>(); | ||
var controllerActivatorProvider = arg1.Services.GetService<IControllerPropertyActivator>(); | ||
((DefaultModelMetadataProvider)modelMetadataProvider).ClearCache(); | ||
((DefaultControllerPropertyActivator)controllerActivatorProvider).ClearCache(); | ||
Console.WriteLine(1111);" | ||
.WithSlimMethodBuilder() | ||
.WithMetadata(typeof(Console)) | ||
.WithUsings("Microsoft.AspNetCore.Mvc.Controllers") | ||
//.WithMetadata(typeof(IgnoresAccessChecksToAttribute)) | ||
.WithMetadata(typeof(IModelMetadataProvider)) | ||
.WithPrivateAccess(typeof(DefaultModelMetadataProvider)) | ||
.ToAction<WebApplication>()!; | ||
action(app); | ||
|
||
// Configure the HTTP request pipeline. | ||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseAuthorization(); | ||
|
||
|
||
app.MapControllers(); | ||
|
||
app.Run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace WebapiWIthController | ||
{ | ||
public class WeatherForecast | ||
{ | ||
public DateTime Date { get; set; } | ||
|
||
public int TemperatureC { get; set; } | ||
|
||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
|
||
public string? Summary { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
samples/HE/NET6.0/WebapiWIthController/WebapiWIthController.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
samples/HE/NET6.0/WebapiWIthController/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Natasha.CSharp/Natasha.CSharp.Compiler/Utils/NatashaAccessHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Metadata; | ||
using System.Text; | ||
|
||
namespace Natasha.CSharp.Compiler.Utils | ||
{ | ||
public static class NatashaAccessHelper | ||
{ | ||
public readonly static Action<AssemblyCSharpBuilder> AccessHandle; | ||
|
||
static NatashaAccessHelper() | ||
{ | ||
var accessType = Assembly.GetEntryAssembly().GetType("System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute", throwOnError: false); | ||
if (accessType == null) | ||
{ | ||
AssemblyCSharpBuilder builder = new(); | ||
builder.UseDefaultLoadContext(); | ||
builder.UseSimpleMode(); | ||
builder.ConfigLoadContext(opt => opt | ||
.AddReferenceAndUsingCode(typeof(object)) | ||
.AddReferenceAndUsingCode(typeof(AssemblyName)) | ||
); | ||
|
||
builder.Add(@" | ||
namespace System.Runtime.CompilerServices | ||
{ | ||
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] | ||
public class IgnoresAccessChecksToAttribute : Attribute | ||
{ | ||
public IgnoresAccessChecksToAttribute(string assemblyName) | ||
{ | ||
AssemblyName = assemblyName; | ||
} | ||
public string AssemblyName { get; } | ||
} | ||
}"); | ||
var assembly = builder.GetAssembly(); | ||
accessType = assembly.GetTypeFromShortName("IgnoresAccessChecksToAttribute"); | ||
} | ||
|
||
AccessHandle = builder => | ||
builder | ||
.ConfigCompilerOption(opt => opt | ||
.WithAllMetadata() | ||
.AppendCompilerFlag(CompilerBinderFlags.IgnoreAccessibility) | ||
) | ||
.ConfigLoadContext(ctx => ctx.AddReferenceAndUsingCode(accessType)); | ||
|
||
} | ||
} | ||
} |