Skip to content
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

Generic attribute GenerateCloneAttribute can cause problems for .NET Framework projects #2245

Open
lietho opened this issue Jul 19, 2024 · 1 comment

Comments

@lietho
Copy link

lietho commented Jul 19, 2024

Describe the bug
Generic attributes were introduced with C# 11 and .NET 7 and are therefore not supported in .NET Framework 4.8 and prior. If another component now tries to read all attributes for the RestResponse<T> class a NotSupportedException is thrown. In our case an additional component not under our control calls Type.GetCustomAttributes for all loaded types.

To Reproduce

  1. Create a .NET Framework 4.8 project
  2. Call typeof(RestResponse<>).GetCustomAttributes()

Expected behavior
No exception should be thrown.

Stack trace
System.NotSupportedException
HResult=0x80131515
Message=Generic types are not valid.
Source=mscorlib
StackTrace:
at System.RuntimeTypeHandle.CreateCaInstance(RuntimeType type, IRuntimeMethodInfo ctor)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
at System.Attribute.GetCustomAttributes(MemberInfo element, Boolean inherit)
at System.Reflection.CustomAttributeExtensions.GetCustomAttributes(MemberInfo element)
at RestSharpTest.Program.Main(String[] args) in C:\Test\RestSharpTest\Program.cs:line 15

Desktop (please complete the following information):

  • OS: Windows 10 22H2
  • .NET version: .NET Framework 4.8
  • Version: 111.3.0+
@lietho lietho added the bug label Jul 19, 2024
@alexeyzimarev
Copy link
Member

alexeyzimarev commented Aug 6, 2024

It's not framework related as RestSharp builds for .NET Framework.

I am not sure if RestSharp has to account for some tools that make calls like that. In addition, the attribute is not even public. It's hard to accommodate for cases where people use reflections to get the library internals and crash because of that.

@alexeyzimarev alexeyzimarev removed the bug label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants