Closed jcouv closed 5 days ago
Hello,
I see that this feature has been merged, however it is still in "Preview". And also it has been added to the C# Next table instead of the C# 10 table. Will this feature make it to the C# 10 release or not?
@csaba-sagi-sonarsource It's waiting until C# 11 due to some issues with the tools and an unresolved runtime issue about how CustomAttributeData.GetCustomAttributes
should handle them. (See https://github.com/dotnet/csharplang/issues/124#issuecomment-898066402 for more details and further discussion.)
Basically, if you install .NET 6 and use <LangVersion>preview</LangVersion>
in your project you should be able to use the feature.
I don't know if this is fully related but the following logic fails when trying to resolve a Generic Attribute using the MetadataReader - it results in an exception of:
Unhandled exception. System.InvalidCastException: Specified cast is not valid.
at System.Reflection.Throw.InvalidCast()
at System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle)
at DNNE.Assembly.Attributors.Attributor.ParseCustomAttribute(MetadataReader reader, CustomAttribute attribute)
at DNNE.Assembly.Attributors.Attributor.IsApplicable(MetadataReader reader, CustomAttribute attribute, Boolean isReturn)
at DNNE.Assembly.AssemblyReader.<>c__DisplayClass12_0.<Read>b__0(IAttributor attributor)
at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
at DNNE.Assembly.AssemblyReader.Read()
at DNNE.Program.Main(String[] args)
Maybe I am doing something wrong below - but I am not sure, can't really find much information about the MetadataReader
and trying to add support for certain features to DNNE
:
The following line throws the exception:
TypeReference refType = reader.GetTypeReference((TypeReferenceHandle)refConstructor.Parent);
which is in:
internal static (StringHandle? parsedNamespace, StringHandle? parsedName) ParseCustomAttribute(MetadataReader reader, CustomAttribute attribute)
{
switch (attribute.Constructor.Kind)
{
case HandleKind.MemberReference:
MemberReference refConstructor = reader.GetMemberReference((MemberReferenceHandle)attribute.Constructor);
if (refConstructor.Parent.IsNil)
{
Debug.Assert(false, "Unknown parent reference kind");
return (null, null);
}
switch (refConstructor.Parent.Kind)
{
case HandleKind.TypeReference:
TypeReference refType = reader.GetTypeReference((TypeReferenceHandle)refConstructor.Parent);
return (refType.Namespace, refType.Name);
default:
Debug.Assert(false, "Unknown parent reference kind");
return (null, null);
}
case HandleKind.MethodDefinition:
MethodDefinition defConstructor = reader.GetMethodDefinition((MethodDefinitionHandle)attribute.Constructor);
TypeDefinition defType = reader.GetTypeDefinition(defConstructor.GetDeclaringType());
return (defType.Namespace, defType.Name);
default:
Debug.Assert(false, "Unknown attribute constructor kind");
return (null, null);
}
}
I am using DotNet 8 and <LangVersion>preview</LangVersion>
inside my project and inside DNNE
Closing out the test plan as the feature has shipped.
If you are still experiencing a problem related to the feature, please file a new issue.
Championed issue Latest LDM notes.
Specification
csharplang
(https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/generic-attributes.md)Compiler
MyAttribute<T>
andMy<T>
are present (bug #54772)where T : INumeric<..>
then argument isIIntegral
Obsolete<T>
)Attribute.GetCustomAttributes(typeof(Program), typeof(Attr<>))
Productivity