AArnott / CodeGeneration.Roslyn

Assists in performing Roslyn-based code generation during a build.
Microsoft Public License
408 stars 59 forks source link

How to identify more about a class definition #72

Closed mwpowellhtx closed 6 years ago

mwpowellhtx commented 6 years ago

Okay, I'd like to glean more details about a class. Based on the front facing example, I can at least tell if I'm dealing with a Class definition. However, how do I determine if it is Partial, what it's lineage is, i.e. what are its parent(s), etc?

Some documentation around methods in general, i.e. Ancestors, would be helpful as well, I think. It means the difference between posting questions here and having the answers there in reference.

Thanks!

mwpowellhtx commented 6 years ago

For instance, what in the world does bool ascendOutOfTrivia mean? And why does it default to true?

mwpowellhtx commented 6 years ago

How would I identify the Ancestors? i.e. parent classes, particularly those that are Generic in nature.

So my lineage looks something like this:

class Tool {}

class Tool<T> : Tool where T : Tool<T> {}

And I would be decorating a class that looked something like this:

class MyTool : Tool<MyTool> {}

And I want to be able to vet whether MyTool was indeed of the expected lineage, Tool, Tool<T>, and so on, before doing anything further.

Thanks!

mwpowellhtx commented 6 years ago

It's turning out this may be a non-starter until the migration path can be resolved.

Specifically, I want to generate some boilerplate operator overloads for the target classes in question, i.e. using OperatorKind, but I do not see this type referenced in the assemblies which could be added as NuGet reference to my project.

See also.

Is there another NuGet package I need to facilitate this?

amis92 commented 6 years ago

The whole domain you're asking about is Roslyn API. This repo is only a tool that uses Roslyn API and allows code generation. We won't answer questions about Roslyn API here. Please refer to it's documentation.

Roslyn is Microsoft.CodeAnalysis namespace. It's a C# compiler platform. Repo is here: https://github.com/dotnet/roslyn

Roslyn docs: https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/

mwpowellhtx commented 6 years ago

Of course, fair enough. My concern as re: CodeGeneration.Roslyn is that it seems to be out of date with the latest Roslyn, so I'm not sure I can leverage Roslyn CodeAnalysis to gen the code I'd like to gen.

My goal is to analyze for target Class, determine whether it is decorated with a known Attribute, and gen a Partial Class by the same name with some helpful overloaded operators, which in turn invoke available Base Class methods. There are a couple of nuances there, I expect, such as alignment within namespaces, responding to refactor requests, class renaming, etc, but which are more Roslyn related than CodeGeneration.

However, I'm finding that perhaps Operator Declaration is a feature that may have been recently added, and the best I could reference without errors is 0.4.11. The latest is 0.4.42, but this would not install on account of incompatibility with Framework 4.6.2, apparently. And 0.4.37 was altogether broken, involving some broken references during dependency resolution.

My context is VS2015 targeting Framework 4.6.2 at the moment. Should I be tarting Framework 4.7.x instead for this to be considered up to date? Please advise...

amis92 commented 6 years ago

I'm not going to deeply analyze your problem (it's not my project), I can only suggest you looking at my Code Generator implementation: https://github.com/amis92/RecordGenerator

Also, Roslyn v2.2.0 (which is what v0.4.42 references) surely supports operators, it's been a fully working C# compiler since v1.0. You just need to search harder, and experiment. It's really all I can say in this matter.

mwpowellhtx commented 6 years ago

@amis92 Thanks for the feedback. Sure, but the documentation is sparse, at best. Needs more elaboration. I'm following up with the Roslyn team along these lines, at any rate. That said, I'll have a look at RecordGenerator. Thanks!

AArnott commented 6 years ago

Updating the Roslyn version used by this project should be fine, FWIW.