Repository navigation
Contextual reflection - #23740
Contextual reflection#23740
Conversation
|
I'll update design doc in a separate PR. There is one unexpected behavior worth noting. Assembly assembly = Assembly.Load("MyAssembly");
using (alc.EnterContextualReflection())
{
Type type = assembly.GetType("MyGeneric`1[[MyType, MyAssembly]]");
Assert.AreEqual(assembly, type.Assembly);
if (alc.LoadFromAssemblyName("MyAssembly") == assembly)
{
Assert.AreEqual(assembly, type.GenericTypeArguments[0].Assembly);
}
else
{
Assert.AreNotEqual(assembly, type.GenericTypeArguments[0].Assembly);
}
}I was tempted to fix this case (to return the same assembly) or throw (if assembly was not in alc & was used as a qualifying name), but I thought it was worth discussing. I decided to leave it at least for now because it felt consistent. Assembly qualified type names are always resolve using the current load context. A similar issue exists with Activator.CreateInstance(null, "MyGeneric`1[[MyType, MyAssembly]]");where the null effectively means |
vitek-karas
left a comment
There was a problem hiding this comment.
Can you please separate the reformatting into a different PR?
The diff is rather large on some files and it's just whitespaces.
I don't mind the change, but I think it should not be part of a PR like this.
|
I guess I don't understand what is the unexpected behavior from the code you posted here. Can you maybe describe it some other way, please? |
e37fd86 to
dcdd98d
Compare
Given // currentContext for assembly.GetType() is effectively
AssemblyLoadContext currentContext =
AssemblyLoadContext.CurrentContextualReflectionContext ??
AssemblyLoadContext.GetLoadContext(assembly);The simple call below will look for Type type = assembly.GetType("MyGeneric`1[[MyType]]");However the following call will look for Type typeQualified = assembly.GetType("MyGeneric`1[[MyType, MyAssembly]]");When assembly != currentContext.LoadFromAssemblyName(assembly.GetName().Name)
I found it interesting/unexpected/surprising. It left me considering what would be the correct behavior.
Current implementation is using option |
|
I played with all the ways the It occurred to me that call semantics seems much less fragile and perhaps more intuitive. I am considering redesigning the API to remove the approved namespace System.Runtime.Loader
{
public partial class AssemblyLoadContext
{
public T EnterContextualReflection(Func<T> function);
public void EnterContextualReflection(Action action);
}
} |
|
I think the most primitive building block is ability to get/set the value. My suggestion would be to make the Once the property settable, the rest is just about convenience APIs. The ones you have suggested look fine to me, but they may not work well for everybody because of they will require lambda allocation - but that's ok since folks can create their own helper in whatever shape they like that just gets and sets the property. |
I'm still concerned it is too primitive. Maybe the setter could be internal, but I think we will have a very hard time controlling the semantics if we directly expose the setter. |
I have been thinking about this further. If I remove the throw, I think the behavior is reasonable and predictable. // Sets CurrentContextualReflectionContext to `alc` for the duration of the block
// Restores to the previous value when exiting the block
using (EnterContextualReflection(alc)) { .... }
// Sets CurrentContextualReflectionContext to `alc` for the duration of the block
// Restores to the previous value when exiting the containing scope
using ContextualReflectionScope scope = EnterContextualReflection(alc);
// Sets CurrentContextualReflectionContext to `alc`
// Restore only if user explicitly calls scope.Dispose()
ContextualReflectionScope scope = EnterContextualReflection(alc);
// Sets CurrentContextualReflectionContext to `alc`
EnterContextualReflection(alc);The last two cases are virtually equivalent to a set operation. It saves the previous value which could help in debugging. |
ff73e33 to
2c0e4f8
Compare
|
test Windows_NT x64 Formatting |
Add using statement tests Add bad usage tests including Assert.Throws<>
Use explicit backing stores for events and properties
7b9e109 to
f262029
Compare
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue dotnet#22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Signed-off-by: dotnet-bot <[email protected]>
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefx#36236 Fix issue #22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject
* Add ContextualReflection APIs Add ContextualReflection APIs approved in dotnet/corefxdotnet/coreclr#36236 Fix issue dotnet/coreclr#22213 * SetParentAssembly even when IsCollectible() * ContextualReflection tests * PR Feedback * Add more usage tests Add using statement tests Add bad usage tests including Assert.Throws<> * Only initialize on set * Add XML API comments * Unify VerifyIsolation * Fix unused expectedAssembly * Remove ContextualReflectionScope throw * Clean up TestResolveMissingAssembly et. al * Remove unused QCall::AppDomainHandle * Remove AppDomainBaseObject * Pass AssemblyLoadContext as managed object to native * Fix AssemblyLoadContextBaseObject packing * AssemblyLoadContext backing stores Use explicit backing stores for events and properties * Remove StaticAsyncLocalCurrentContextualReflectionContext * Remove PermissionSetObject Commit migrated from dotnet/coreclr@206f968
Add ContextualReflection APIs approved in dotnet/corefx#36236
Fix issue #22213
Fix SetParentAssembly even when IsCollectible()
Add ContextualReflection test