daver32 / interfacegenerator Goto Github PK
View Code? Open in Web Editor NEWA simple source generator that creates interfaces by implementations.
License: MIT License
A simple source generator that creates interfaces by implementations.
License: MIT License
We've started using records a lot for services because not having to write out the constructor and all the private readonly
fields is convenient.
It would be nice to have the option to exclude those things from the generated interface. Specifically:
void Deconstructor(out param1, out param2, ...
)get
+ init
)I'm surprised the Equals and GetHashCode methods didn't get included as well, but I'm fine with that.
Basically, could the extension be .g.cs
instead of just .cs
which seems to be some convention for source generated files.
Coveralls will exclude these as well. I can't get any work around manually to exclude them to work.
coverlet-coverage/coverlet#1084
Thanks!
Hi,
Possibly I have overlooked something, but I need some help getting started to start using this project (because I think the concept is really great).
I have added the Nuget package to a NET6 class library project and added the GenerateAutoInterface
attribute to my class, but VS cannot find any reference.
Within the readme an other project net_automatic_interface is mentioned, There you need to add the code for the attribute manually.
Do you also need to do this for this project?
Or is NET6 not supported?
Kr
Edward
I get conflicts when I also install: https://github.com/CollinAlpert/Lombok.NET
Hey,
while testing this source generator with another one I noticed an issue with some generated parameter names.
Methods that use keywords such as long
as identifiers don't get properly prefixed with @
in the generated interface.
Example:
public void VoidMethodWithKeywordParam(long @long)
results in:
void VoidMethodWithKeywordParam(long long); // CS1001 Identifier expected
but should be:
void VoidMethodWithKeywordParam(long @long);
This should be the line where param.Name
needs to be checked and sometimes prefixed.
e. g. AutoRest is handling these identifiers like this:
1. Writing the identifier.
2. Checking for C# keywords
Curent Behaviour:
For the class
[InterfaceGenerator.GenerateAutoInterface(VisibilityModifier = "public")]
public class AClass : IAClass
{[return: NotNullIfNotNull(nameof(content))] public byte[]? TheMethod(byte[]? content) { if (content == null) return content; return content; } }
this interface is generated:
public partial interface IAClass { byte[]? TheMethod(byte[]? content); }
Excpected Interface:
public partial interface IAClass { [return: NotNullIfNotNull(nameof(content))] byte[]? TheMethod(byte[]? content); }
Also other paremeter Attributes should be considered. Eg. MaybeNullWhen() or EnumeratorCancellation.
A property defined as
public int[,] Prop;
Generates in the interface file causing a compilation error.
int[*,*];
This library seems not to support init only setters from c# 9
Tested on 1.0.5 on net6.0
Given this code:
[InterfaceGenerator.GenerateAutoInterface]
class Class : IClass
{
public string Property { get; init; }
}
AutoInterface creates this interface:
internal partial interface IClass
{
string Property { get; set; }
}
but it should be:
internal partial interface IClass
{
string Property { get; init; }
}
probably better to use NET6 instead of netstandard? can netstandard cause any issues moving forward
We have our own custom set of log events that we keep track of in a database. The severity is stored with a custom LogLevel enum:
public enum LogLevel
{
Trace = 0,
Debug = 1,
Information = 2,
Warning = 3,
Error = 4,
Critical = 5,
None = 6
}
For the DAL class, we have a method that has this format:
public async Task<IEnumerable<LogMessage>> GetLogsForObject(string objectType, long objectId, LogLevel minLogLevel = LogLevel.Information)
It generates the following interface:
public partial interface ILogMessageRepository
{
System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<TSWCloud.Domain.Logging.LogMessage>> GetLogsForObject(string objectType, long objectId, TSWCloud.Domain.Logging.LogLevel minLogLevel = 2);
}
When compiling, this gives the error:
A value of type 'int' cannot be used as a default parameter because there is no standard conversions to type LogLevel
Instead, it should generate the following interface:
public interface ILogMessageRepository
{
Task<IEnumerable<LogMessage>> GetLogsForObject(string objectType, long objectId,
LogLevel minLogLevel = LogLevel.Information);
}
Currently the VisibilityModifier option on the GenerateAutoInterfaceAttribute is of type string. I think it would be better to be able to define this setting using an enum.
Theoretically there could be multiple access modifiers. For example "protected internal:". Therefor the enum should have the [Flags] attribute to be able to define multiple access modifiers or just include all allowed combinations.
I noticed that XML docs are copied to the interface but only for members. They are not copied to the interface itself. Maybe they should be also copied when XML docs are defined on the class?
If possible, it would great if the nullable context (off, annotations, on) could be set per item on the interface to match whatever has been set on the implementation with #nullable enable
and the like.
Right now, if I set something to a nullable reference type, I get a build warning.
The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable'
This library (1.0.5 on net6.0) works perfectly for me, thanks for the great work!
What I stumbled upon using it was, that records are not supported. It may need some fine tuning to properly support them though.
// records don't work at all for now
[InterfaceGenerator.GenerateAutoInterface]
record Record : IRecord
{
public void Method(){}
}
// I expect it to generate an interface like that:
internal partial interface IRecord
{
string Property { get; init; }
void Method();
void Deconstruct(out string Property);
}
// classes work fine
[InterfaceGenerator.GenerateAutoInterface]
class Class : IClass
{
public string Property { get; set; }
public void Method(){}
}
// this generates a proper interface:
internal partial interface IClass
{
string Property { get; set; }
void Method();
}
Most of the generated files have this comment on top:
// <auto-generated/>
Then, code analysis tools know to don't show warnings about this code. This library doesn't add this comment and because of that, I have a lot of warnings to GenerateAutoInterfaceAttribute (I don't have any warnings to files with generated interfaces) (some of the warnings are against my configuration, I don't know why).
I've noticed that if using InterfaceGenerator, the following libraries appear in the bin folder (17.5 MB size total)
Note that if I use https://github.com/codecentric/net_automatic_interface then these libraries are not referenced, so this must be a bug.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.