-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Cannot read MarshalAsAttribute in source generator #54754
Comments
TL;DR: This is happening because I don't have a great workaround for you, but I can at least explain why it's happening (and why you might not hold your breath on this feature getting added any time soon.)
Unfortunately since Roslyn doesn't have a need for marshaling info, it doesn't parse it when it comes from an external source. The reason this issue doesn't come up with reflection is the runtime synthesizes fake attributes. In theory Roslyn could do this too, but I'm not sure how the devs would feel about it. As for workarounds potential workarounds... If all you need is the If you need anything beyond that, I believe the rest is hiding in its encoded form in |
Thanks for detailed answer. I was aware that MarshalAs is pseudo-custom attributes. And rationale why it is not included make sense. What I thinking right now, is that time changed with source generators. One use-case for source generators is implementing efficient marshalling for .NET and for generation of COM RCW/CCW using ComWrappers. For that you may need declaration of structures and interfaces from other assemblies. For now, I'm probably would be happy with proposed workarounds. But isn't this should be handled properly in Roslyn nowadays? |
I think the best path forward here is to get an API proposal together for how we expose pseudo attributes in Roslyn. @333fred as I believe this has come up before. Not sure if we have an existing proposal that we can add onto here. |
This is similar to others, but we don't have anything specifically related to MarshalAs requested yet. We decode this information (https://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Symbols/FieldSymbol.cs,291), but it is not exposed in any public form today. We'd need an API proposal for how to expose marshalling information. |
Initially I was thinking that somehow when calling public interface IParameterSymbol
{
+ MarshallingInformation GetMarshallingInformation();
}
public interface IFieldSymbol
{
+ MarshallingInformation GetMarshallingInformation();
}
+public struct MarshallingInformation
+{
+ public System.Runtime.InteropServices.UnmanagedType MarshalType { get; internal set; }
+ public System.Runtime.InteropServices.UnmanagedType ArrayElementType { get; internal set; }
+ public int ArraySizeConst { get; internal set; }
+ public int ArraySizeParameterIndex { get; internal set; }
+ public ISymbol CustomMarshallerType { get; internal set; }
+ public string CustomMarshallerCookie { get; internal set; }
+ public int ComInterfaceIIDParameterIndex { get; internal set; }
+} Depending on the implementation of |
Version Used:
3.10.0
Steps to Reproduce:
[MarshalAs]
attribute assigned to parameter. Save toIParameterSymbol parameterSymbol
variable.parameterSymbol.GetAttributes()
[MarshalAs]
present.Important that method should be in the separate assembly, If method is in same assembly in which source generator produce code, then
MarshalAsAttribute
appears in the list of attributes.The text was updated successfully, but these errors were encountered: