-
Description To Reproduce
Version Info
Additional Info Can the attributes have the |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
Maybe a silly question, but why? The attribute is there so that the source generator knows to process those fields. That only happens if the source generator is actually referenced by the project which is using the attribute. If you were able to reference the attribute from another project, the source generator wouldn't notice, because it wouldn't be processing that other project. |
Beta Was this translation helpful? Give feedback.
-
Makes sense, this sounds to me more like a limitation of source generators in general and not specifically for this source generator. |
Beta Was this translation helpful? Give feedback.
-
On the other hand, source generators are a build-time dependency: they're there to aid the compilation process. If a project references a source generator package, the compiled assembly won't reference anything, and it won't add any dlls to your bin folder. Just because one project happens to use a source generator to help it build, doesn't necessarily mean that any projects which reference it want to use that same source generator: indeed, forcing that source generator on them might break something! For instance, there are quite a few INPC source generators, and it would be annoying if you were forced to use a particular one just because one of your dependencies decided to use it internally. I'd call this a good design decision, rather than a limitation of SGs. If your Project 2 wants to use a particular SG, it just has to reference it. |
Beta Was this translation helpful? Give feedback.
-
The CommunityToolkit provides this feature (i.e: I can reference Project 2 in Project 1 and have the attributes generate the code). Personally, I find this useful for maintenance. Otherwise I have to go to every project and reference the NuGet package, which becomes an issue for large solutions. I'm reopening this issue since shouldn't have rushed to close it. |
Beta Was this translation helpful? Give feedback.
-
This is because Microsoft.Toolkit.Mvvm is a nuget package which contains both DLLs and source generators. As such, it doesn't get a You can see the source generators in the referencing project under Dependencies -> Analyzers in the Solution Explorer. The SG is installed into your referencing project: it's just that it was inherited from a referenced project, rather than explicitly installed. You can remove the But, the attributes still need to stay internal in this case. Otherwise you'll have one version of the attributes emitted into the dependency, and a second version with the same name and namespace emitted into the project which depends on it, and any attempt to reference that attribute will raise a warning because it's ambiguous. Note that in the community toolkit, the attributes are put into a separate assembly, and aren't emitted into the project directly. This means that all of your projects reference the same dll and have access to the same version of the attributes. So you don't have the problems which come from each project having its own definition of the attributes. I specifically don't want PropertyChanged.SourceGenerator to add any runtime dependencies. The attributes are only needed during compilation: they're just useless baggage after that, and forcing the user to reference a dll just to be able to see these attributes which have no use at runtime is a waste. It sounds like removing Making the attributes public while keeping |
Beta Was this translation helpful? Give feedback.
-
Also, if all of your projects have the same set of dependencies, you can define them in a You can either put the |
Beta Was this translation helpful? Give feedback.
This is because Microsoft.Toolkit.Mvvm is a nuget package which contains both DLLs and source generators. As such, it doesn't get a
<PrivateAssets>all</PrivateAssets>
property in the csproj, which means that its source generators are exported to any projects which reference it.You can see the source generators in the referencing project under Dependencies -> Analyzers in the Solution Explorer. The SG is installed into your referencing project: it's just that it was inherited from a referenced project, rather than explicitly installed.
You can remove the
<PrivateAssets>all</PrivateAssets>
from thePackageReference
property for PropertyChanged.SourceGenerator in your csproj if you want, an…