Comments (7)
Here's a naff workaround I concocted (I have this in the .csproj file for my
project):
<ItemGroup>
<ProtoFiles Include="Protos\*.proto" />
</ItemGroup>
<UsingTask TaskName="FixupGeneratedFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text"/>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var compilerGeneratedText = "[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute]";
foreach (var file in Files)
{
var text = File.ReadAllText(file.ItemSpec);
var sb = new StringBuilder(text);
var offset = 0;
var matches = Regex.Matches(text, @"(public|internal).*class");
foreach (Match match in matches)
{
sb.Insert(match.Index + offset, compilerGeneratedText);
offset += compilerGeneratedText.Length;
}
sb.Insert(0, "#pragma warning disable");
sb.AppendLine();
sb.AppendLine("#pragma warning restore");
sb.Insert(0, "// <auto-generated />" + Environment.NewLine);
File.WriteAllText(file.ItemSpec, sb.ToString());
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="BeforeBuild">
<CallTarget Targets="ProtoCompile;ProtoGenerate" />
</Target>
<Target Name="ProtoCompile" Inputs="@(ProtoFiles)" Outputs="Protos\PF.protobin">
<Message Text="Compiling .proto files" Importance="Low" />
<Exec Command=""..\..\Lib\protobuf-csharp\protoc.exe" -o Protos\PF.protobin --proto_path=Protos @(ProtoFiles -> 'Protos\%(Filename)%(Extension)', ' ')" />
</Target>
<Target Name="ProtoGenerate">
<Message Text="Generating C# code from compiled .protos" Importance="Low" />
<Exec Command=""..\..\Lib\protobuf-csharp\protogen.exe" Protos\PF.protobin -namespace=Xxx.Protos -output_directory=Protos" />
<ItemGroup>
<GeneratedFiles Include="Protos\*.cs" />
</ItemGroup>
<FixupGeneratedFiles Files="@(GeneratedFiles)"/>
</Target>
Original comment by [email protected]
on 1 Aug 2011 at 10:17
from protobuf-csharp-port.
1. Can you give details of the warnings generated? I'd rather not suppress
warnings, as it may indicate a problem with the generated code. We could make
it an option if really necessary though.
2. I'm not sure about [CompilerGenerated] - are there any other attributes
which would make sense? I can't remember offhand what the *designer* generated
code ends up like...
3. Sounds good to me. Right at the very top of the file?
Original comment by jonathan.skeet
on 4 Aug 2011 at 7:58
from protobuf-csharp-port.
Thanks for the reply.
1. If you have XML documentation enabled for your project, you get warnings
about missing API doc comments. That's the only one I've found thus far, so
maybe you could just disable those specific warnings (#1591).
2. Good point - I hadn't considered the fact that it's a partial class so not
all of it may be generated. CompilerGeneratedAttribute can target anything, so
you could generate this for every generated *member* instead.
The Winforms designer just wraps generated code in a region, but this doesn't
(by default anyway) help code analysis to detect that it's generated code. If I
stick a [CompilerGenerated] attribute on the InitializeComponent method, CA
ignores it as expected. Sounds like people generally modify the template to
include this attribute (see
http://stackoverflow.com/questions/2768764/attribute-missing-from-generated-winf
orm-code).
3. Yes, the very top.
On a side note, I really wish all relevant tooling would respect the one way of
marking code as generated!
Original comment by [email protected]
on 5 Aug 2011 at 8:21
from protobuf-csharp-port.
Implemented some of the suggestions:
1. Added #pragma warning disable 1591
2. The GeneratedCodeAttribute, CompilerGeneratedAttribute, and
NonUserCodeAttribute have already been added.
3. Added code region for "Designer generated code". Resharper respects this
and disables warnings. I did not add the suggested comment "// <auto-generated
/>" as I can find no reference to it's use online. If the code region is not
working in a specific tool, and you can provide the product documentation link
about this comment we will get it integrated.
Pending review:
http://code.google.com/p/protobuf-csharp-port/source/list?name=issue-16
Original comment by [email protected]
on 13 Aug 2011 at 1:20
- Changed state: Started
from protobuf-csharp-port.
[deleted comment]
from protobuf-csharp-port.
This mentioned <auto-generated>:
http://shishkin.wordpress.com/2008/07/08/stylecop-how-to-ignore-generated-code/
It says that the region change should fix it too though, so let's see whether
that works and revisit it if necessary.
Original comment by jonathan.skeet
on 13 Aug 2011 at 6:28
from protobuf-csharp-port.
The requested changes have been completed.
Original comment by [email protected]
on 13 Aug 2011 at 11:30
- Changed state: Fixed
from protobuf-csharp-port.
Related Issues (20)
- "GenericArguments[0], 'SystemMessageTest', on 'Google.ProtocolBuffers.IBuilderLite`2[TMessage,TBuilder]' violates the constraint of type parameter 'TMessage'." HOT 3
- .NET Core support HOT 2
- Problem with required enum field with default value HOT 3
- Problem with Protobuf on IOS HOT 2
- fig
- Build protobuf-csharp-port with VS2015 HOT 1
- repeated member of message with suffix "List" cause compile error HOT 1
- How to access GetExtension in c# like c++ HOT 3
- out of memory HOT 5
- No support for enums 'allow_alias' feature HOT 1
- src/ProtoBench/Program.cs(254,65): error CS0103: The name `JsonReaderWriterFactory' does not exist in the current context HOT 1
- Patch for /src/ProtocolBuffers/Properties/AssemblyInfo.cs HOT 10
- Unknown fields are not serialized to XML or JSON formats HOT 6
- csharp_options.proto breaks python HOT 4
- Deserialization ignores a nonempty field HOT 6
- Support new oneof feature HOT 11
- Custom Options with more than one required field can't be used HOT 8
- CodedInputStream.ReadEnum performance HOT 3
- How can I use csharp-port 2.4 with mono in IOS? It always got a Jit error message. HOT 9
- Builder.Clear() does not clear enum fields HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf-csharp-port.