Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Contextual reflection - #23740

Merged
sdmaclea merged 18 commits into
dotnet:masterfrom
sdmaclea:ContextualReflection
Apr 11, 2019
Merged

sdmaclea merged 18 commits into
dotnet:masterfrom
sdmaclea:ContextualReflection

Conversation

@sdmaclea

@sdmaclea sdmaclea commented Apr 4, 2019

Copy link
Copy Markdown

Add ContextualReflection APIs approved in dotnet/corefx#36236
Fix issue #22213
Fix SetParentAssembly even when IsCollectible()
Add ContextualReflection test

@sdmaclea

sdmaclea commented Apr 4, 2019 •

Copy link
Copy Markdown
Author

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 assembly = Assembly.GetExecutingAssembly()

@vitek-karas vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/vm/typeparse.cpp Resolved
@vitek-karas

Copy link
Copy Markdown
Member

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?

@sdmaclea
sdmaclea force-pushed the ContextualReflection branch from e37fd86 to dcdd98d Compare April 5, 2019 21:14
@sdmaclea

sdmaclea commented Apr 5, 2019 •

Copy link
Copy Markdown
Author

what is the unexpected behavior from the code you posted here

Assembly.GetType() typically does not allow assembly qualified types. For most types it simply looks for the type within itself. For generics, it allows assembly qualified types for the GenericTypeArguments.

Given

// currentContext for assembly.GetType() is effectively
AssemblyLoadContext currentContext = 
    AssemblyLoadContext.CurrentContextualReflectionContext ??
    AssemblyLoadContext.GetLoadContext(assembly);

The simple call below will look for MyGeneric and MyType within assembly MyAssembly.

Type type = assembly.GetType("MyGeneric`1[[MyType]]");

However the following call will look for MyGeneric within assembly, but look for MyType within the MyAssembly assembly returned by the currentContext.

Type typeQualified = assembly.GetType("MyGeneric`1[[MyType, MyAssembly]]");

When

assembly != currentContext.LoadFromAssemblyName(assembly.GetName().Name)

typeQualified becomes an odd corner case where the type has two references to MyAssembly each from a different AssemblyLoadContext.

I found it interesting/unexpected/surprising. It left me considering what would be the correct behavior.

  1. Throwing if assembly != currentContext.LoadFromAssemblyName(assembly.GetName().Name)
  2. Stripping assembly.GetName().Name from GenericTypeArguments
  3. Allowing the semantic to be, assembly qualified names will be determined by the currentContext.
  • Option 1 would be an expensive check which would throw too often.
  • Option 2 felt heavy handed.
  • Option 3 seemed consistent and logical.

Current implementation is using option 3. It seems best.

@sdmaclea

sdmaclea commented Apr 5, 2019

Copy link
Copy Markdown
Author

I played with all the ways the IDisposable semantics could be broken. It is as fragile as @jkotas was suggesting in the API review.

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 AssemblyLoadContext.EnterContextualReflection(). Replacing it with call semantics:

namespace System.Runtime.Loader
{
    public partial class AssemblyLoadContext
    {
        public T EnterContextualReflection(Func<T> function);
        public void EnterContextualReflection(Action action);
    }
}

@jkotas

jkotas commented Apr 5, 2019

Copy link
Copy Markdown
Member

I think the most primitive building block is ability to get/set the value. My suggestion would be to make the CurrentContextualReflectionContext property settable.

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.

@sdmaclea

sdmaclea commented Apr 6, 2019

Copy link
Copy Markdown
Author

most primitive building block is ability to get/set the value

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.

@sdmaclea

sdmaclea commented Apr 6, 2019

Copy link
Copy Markdown
Author

It is as fragile as @jkotas was suggesting in the API review.

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.

@sdmaclea
sdmaclea force-pushed the ContextualReflection branch from ff73e33 to 2c0e4f8 Compare April 8, 2019 18:27
@sdmaclea

sdmaclea commented Apr 9, 2019

Copy link
Copy Markdown
Author

test Windows_NT x64 Formatting

Comment thread src/vm/mscorlib.h Resolved
Comment thread src/vm/vars.hpp Resolved
@sdmaclea
sdmaclea force-pushed the ContextualReflection branch from 7b9e109 to f262029 Compare April 11, 2019 00:05
@sdmaclea
sdmaclea merged commit 206f968 into dotnet:master Apr 11, 2019
@sdmaclea
sdmaclea deleted the ContextualReflection branch April 11, 2019 04:20
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corefx that referenced this pull request Apr 11, 2019
* 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]>
stephentoub pushed a commit to dotnet/corefx that referenced this pull request Apr 12, 2019
* 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]>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Apr 12, 2019
* 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]>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/mono that referenced this pull request Apr 12, 2019
* 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]>
marek-safar pushed a commit to mono/mono that referenced this pull request Apr 12, 2019
* 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]>
jkotas pushed a commit to dotnet/corert that referenced this pull request Apr 14, 2019
* 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]>
davmason pushed a commit to davmason/coreclr that referenced this pull request Apr 27, 2019
* 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
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants