@@ -1822,6 +1822,36 @@ cattr_class_match (MonoClass *attr_klass, MonoClass *klass)
18221822 (m_class_is_gtd (attr_klass ) && m_class_is_ginst (klass ) && mono_class_is_assignable_from_internal (attr_klass , mono_class_get_generic_type_definition (klass )));
18231823}
18241824
1825+ /*
1826+ * Helper function to filter out internal/compiler-generated attributes
1827+ * that should not be visible through reflection to match CoreCLR behavior
1828+ */
1829+ static gboolean
1830+ should_filter_attribute_for_reflection (MonoMethod * ctor )
1831+ {
1832+ MonoClass * attr_class = ctor -> klass ;
1833+ const char * attr_name = m_class_get_name (attr_class );
1834+ const char * attr_namespace = m_class_get_name_space (attr_class );
1835+
1836+ // Filter out these specific attributes from System.Object reflection
1837+ if (strcmp (attr_namespace , "System.Runtime.CompilerServices" ) == 0 ) {
1838+ if (strcmp (attr_name , "NullableContextAttribute" ) == 0 ||
1839+ strcmp (attr_name , "TypeForwardedFromAttribute" ) == 0 ||
1840+ strcmp (attr_name , "IsReadOnlyAttribute" ) == 0 ) {
1841+ return TRUE;
1842+ }
1843+ }
1844+
1845+ if (strcmp (attr_namespace , "System.Runtime.InteropServices" ) == 0 ) {
1846+ if (strcmp (attr_name , "ClassInterfaceAttribute" ) == 0 ||
1847+ strcmp (attr_name , "ComVisibleAttribute" ) == 0 ) {
1848+ return TRUE;
1849+ }
1850+ }
1851+
1852+ return FALSE;
1853+ }
1854+
18251855static MonoArrayHandle
18261856mono_custom_attrs_construct_by_type (MonoCustomAttrInfo * cinfo , MonoClass * attr_klass , MonoError * error )
18271857{
@@ -1847,11 +1877,24 @@ mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_
18471877 for (i = 0 ; i < cinfo -> num_attrs ; ++ i ) {
18481878 MonoMethod * ctor = cinfo -> attrs [i ].ctor ;
18491879 g_assert (ctor );
1850- if (cattr_class_match (attr_klass , ctor -> klass ))
1880+ if (cattr_class_match (attr_klass , ctor -> klass )) {
1881+ // Apply filtering for System.Object attributes
1882+ if (should_filter_attribute_for_reflection (ctor )) {
1883+ continue ; // Skip this attribute
1884+ }
18511885 n ++ ;
1886+ }
18521887 }
18531888 } else {
1854- n = cinfo -> num_attrs ;
1889+ // Count attributes while applying filtering
1890+ for (i = 0 ; i < cinfo -> num_attrs ; ++ i ) {
1891+ MonoMethod * ctor = cinfo -> attrs [i ].ctor ;
1892+ g_assert (ctor );
1893+ // Apply filtering for System.Object attributes
1894+ if (!should_filter_attribute_for_reflection (ctor )) {
1895+ n ++ ;
1896+ }
1897+ }
18551898 }
18561899
18571900 result = mono_array_new_cached_handle (mono_defaults .attribute_class , n , error );
@@ -1860,6 +1903,10 @@ mono_custom_attrs_construct_by_type (MonoCustomAttrInfo *cinfo, MonoClass *attr_
18601903 for (i = 0 ; i < cinfo -> num_attrs ; ++ i ) {
18611904 MonoCustomAttrEntry * centry = & cinfo -> attrs [i ];
18621905 if (!attr_klass || cattr_class_match (attr_klass , centry -> ctor -> klass )) {
1906+ // Apply filtering for System.Object attributes
1907+ if (should_filter_attribute_for_reflection (centry -> ctor )) {
1908+ continue ; // Skip this attribute
1909+ }
18631910 create_custom_attr_into_array (cinfo -> image , centry -> ctor , centry -> data ,
18641911 centry -> data_size , result , n , error );
18651912 goto_if_nok (error , exit );
0 commit comments