You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type.GetType("TypeName") will pick the "first" such type found #98955
If we're resolving type name -> TypeDefinition (for example Type.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 runtime behavior in this case is to search for MyType in assembly B. But currently (without cross-method analysis) there's no way for the linker to figure this out. At the time we're trying to resolve MyType we only see assembly A.
I'm curious if there are cases where we really need to support non-qualified type names flowing into DynamicallyAccessedMembers. If we don't have a way to find the right type, maybe it's better to warn instead of marking all types with the same name.
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 typeof since the wrapper assembly and corelib will already be referenced. In cases where there are nested wrappers from different assemblies this might not be true, but this seems like a fragile setup in the first place.
// will cache the assembly based on identity, however,
// so it could be looking in the previous version
// of the assembly and not finding the type. We work
// around this by looking for the non-assembly qualified
// name, which causes the domain to raise a type
// resolve event.
//
t=Type.GetType(typeName.Substring(0,commaIndex));
}
}
returnt;
}
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.
maybe it's better to warn instead of marking all types with the same name.
I wouldn't be against this if the linker can't figure it out.
but this seems like a fragile setup in the first place.
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).
A known string is flowing into a Type.GetType call. We know the calling assembly, so we can correctly predict what the runtime is going to do. Allow unqualified strings for convenience.
A known string is flowing into a location with DynamicallyAccessedMembers. If it's unqualified, generate a warning and do nothing.
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.
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 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.
Activity
vitek-karas commentedon Mar 15, 2021
/cc @eerhardt
sbomer commentedon Mar 15, 2021
I believe for non-qualified type names,
GetTypewill only look in the calling assembly or in corelib - we might want to do the same in the linker.vitek-karas commentedon Mar 15, 2021
Yeah - I know, the problem is tricky though.
The runtime behavior in this case is to search for
MyTypein assemblyB. But currently (without cross-method analysis) there's no way for the linker to figure this out. At the time we're trying to resolveMyTypewe only see assemblyA.sbomer commentedon Mar 15, 2021
I'm curious if there are cases where we really need to support non-qualified type names flowing into
DynamicallyAccessedMembers. If we don't have a way to find the right type, maybe it's better to warn instead of marking all types with the same name.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
typeofsince the wrapper assembly and corelib will already be referenced. In cases where there are nested wrappers from different assemblies this might not be true, but this seems like a fragile setup in the first place.eerhardt commentedon Mar 15, 2021
Check out these places from ComponentModel.TypeConverter:
runtime/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.ReflectedTypeData.cs
Lines 482 to 521 in 82ca681
runtime/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/ReflectTypeDescriptionProvider.cs
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.
vitek-karas commentedon Mar 15, 2021
+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).
MichalStrehovsky commentedon Mar 16, 2021
We could potentially recognize two situations:
Type.GetTypecall. We know the calling assembly, so we can correctly predict what the runtime is going to do. Allow unqualified strings for convenience.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.
marek-safar commentedon Mar 16, 2021
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.
sbomer commentedon Aug 4, 2021
@mateoatr can this be closed?
mateoatr commentedon Aug 4, 2021
Yeah,
TypeNameResolvernow follows the behavior ofType.GetTypewhen resolving a type from a type string (see here). Closing.sbomer commentedon Feb 26, 2024
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.
sbomer commentedon Jun 26, 2024
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.
Fix type parsing issues in ILLink and ILC (dotnet#104060)