-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type.GetType("TypeName") will pick the "first" such type found #98955
Comments
/cc @eerhardt |
I believe for non-qualified type names, |
Yeah - I know, the problem is tricky though. // Assembly A
public void Test()
{
LoadRandomType("MyType");
}
// Assembly B
public object LoadRandomType([DynamicallyAccessedMembers(PublicConstructors)] string typeName)
{
Activator.CreateInstance(Type.GetType(typeName));
} The runtime behavior in this case is to search for |
I'm curious if there are cases where we really need to support non-qualified type names flowing into Anyone wrapping reflection APIs this way will already be facing the limitation that the wrapper only works for certain non-qualified type names (those that exist in the wrapper assembly or corelib) - it might be useful for this to produce a warning even independently of trimming. I think it should be possible to convert most of these cases to use |
Check out these places from ComponentModel.TypeConverter: Lines 482 to 521 in 82ca681
Lines 914 to 941 in 82ca681
All the callsites to these methods are from attributes that allow the string type name to be passed to its constructor. I'm annotating those attributes with DynamicallyAccessedMembers in #49467.
I wouldn't be against this if the linker can't figure it out.
Agreed. The above code looks like it mostly is working around odd design-time issues in .NET Framework. |
+1 on what @eerhardt said. My worry is mostly about old code, which happens to work. The current behavior of the linker in this case is just wrong - on the other hand it probably works lot of time (having the same namespace.type name in multiple assemblies is not very common). |
We could potentially recognize two situations:
I'm not sure how common 1 would be because as Sven pointed out, people can just do typeof. We could just scope it out and keep it in mind if we get feedback. |
I think we should just issue a warning. There seems to be an easy workaround with typeof or with the fully qualified name. IF this really becomes a problem in wild we can work on the heuristic but the warning will most likely stay there for the ambiguous cases anyway. |
@mateoatr can this be closed? |
Yeah, |
Reopening this because illink isn't yet producing a warning when an unqualified type name flows into location with DynamicallyAccessedMembers. The current behavior is to warn only if we fail to resolve an unqualified type name (by looking in the "current" assembly and corelib). But the "current" assembly where the type name flows into a location with DynamicallyAccessedMembers might be different than the assembly where Type.GetType is called. ILC seems to have the desired behavior. |
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsIf we're resolving type name -> There's no guarantee it's the right one (if there are multiple). It would be easy to write a counter example.
|
Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas Issue DetailsIf we're resolving type name -> There's no guarantee it's the right one (if there are multiple). It would be easy to write a counter example.
|
ILC wasn't doing this right either - it was only warning when failing to resolve a non-fully-qualified type name. Which meant it might resolve the type from the wrong assembly if the callsite where the string flowed into a DAM-annotated location was in a different assembly than the Type.GetType call. Fixing this in #104060. |
If we're resolving type name ->
TypeDefinition
(for exampleType.GetType
does this), and the type name is not assembly qualified, we will iterate over all assemblies and search each one for that type name. Once we find the first, we'll use that.There's no guarantee it's the right one (if there are multiple). It would be easy to write a counter example.
The text was updated successfully, but these errors were encountered: