cysharp / privateproxy Goto Github PK
View Code? Open in Web Editor NEWSource Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
License: MIT License
Source Generator and .NET 8 UnsafeAccessor based high-performance strongly-typed private accessor for unit testing and runtime.
License: MIT License
I managed to reproduce something similar to this library in the Unity environment and managed to apply it to generics and static classes as well.
https://github.com/Akeit0/ILAttributes
I believe ref fields can also be applied by wrapping a ref into a ref struct.
However, since this is using IL Weaving, this is not as simple as it sounds.
Do you intend to introduce this or wait for formal support?
Pretty much the title. It would be nice to have support for constructors. However, this may not fit the current extension method approach... but extern method could be exposed directly.
using PrivateProxy;
public class Sample
{
private Sample(int x) {}
}
[GeneratePrivateProxy(typeof(Sample))]
partial struct SampleProxy;
// Generated
partial struct SampleProxy(Sample target)
{
[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
public static extern Sample CreateSample(int x);
}
BTW thank you for this nice source generator!
In this part, parameters public checks do nothing. (foreach continue)
else if (generateMethod && item is IMethodSymbol m)
{
// both return type and parameter type must be public
if (m.ReturnType.DeclaredAccessibility != Accessibility.Public) continue;
foreach (var parameter in m.Parameters)
{
if (parameter.Type.DeclaredAccessibility != Accessibility.Public) continue;
}
if (m.DeclaredAccessibility == Accessibility.Public) continue;
list.Add(new(item));
}
And only checking Accessibility.Public doesn't care about element types.
Following is what I'm using in my library.
static bool CanExpose(ITypeSymbol typeSymbol)
{
if (typeSymbol is IPointerTypeSymbol) return false;
while(typeSymbol is IArrayTypeSymbol arrayType)
{
typeSymbol = arrayType.ElementType;
if (typeSymbol is IPointerTypeSymbol) return false;
}
if(typeSymbol is INamedTypeSymbol namedTypeSymbol)
{
if (namedTypeSymbol.IsGenericType)
{
foreach (var e in namedTypeSymbol.TypeArguments)
{
if (!CanExpose(e)) return false;
}
}
}
var declaredAccessibility = typeSymbol.DeclaredAccessibility;
switch (declaredAccessibility)
{
case Accessibility.Public:
return true;
case Accessibility.NotApplicable:
return true;
default:
return false;
}
}
別プロジェクトのクラスのPrivateProxyを生成しようとすると、VisualStudio上ではエラーになりませんがビルドするとCS1061が発生します。
次のリポジトリに最小限のプロジェクトをおいています。
When trying to generate a private proxy for System.Text.Json.JsonElement
,
[GeneratePrivateProxy(typeof(JsonElement))]
public ref partial struct JsonElementProxy;
it seems to generate a dummy/garbage implementation.
ref partial struct JsonElementProxy
{
ref global::System.Text.Json.JsonElement target;
public JsonElementProxy(ref global::System.Text.Json.JsonElement target)
{
this.target = ref target;
}
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_dummy")]
static extern ref readonly object ___dummy__(ref global::System.Text.Json.JsonElement target);
public ref readonly object _dummy => ref ___dummy__(ref this.target);
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_dummyPrimitive")]
static extern ref readonly int ___dummyPrimitive__(ref global::System.Text.Json.JsonElement target);
public ref readonly int _dummyPrimitive => ref ___dummyPrimitive__(ref this.target);
}
Any help or guidance would be appreciated!
Even though UnsafeAccessorAttribute
is new to .NET 8, accessing private members with no overhead has always been possible in older .NET runtimes through the use of IgnoresAccessChecksToAttribute
. That are a couple of NuGet packages (like MakeTypesPublic) that provide MSBuild targets to automate all steps required to use it.
My suggestion is that, it would be very nice if the Source Generator could provide a fallback, and generate proxies that use this attribute instead, when building in .NET versions where UnsafeAccessorAttribute
is not available.
Unfortunately, though, you can't use IgnoresAccessChecksToAttribute
to unlock access inside your own assembly. But I suppose if someone wants that, they can just edit their own source code and make things internal
, so this shouldn't be an issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.