Generate Compile Time Error C#
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and c# #error policies of this site About Us Learn more about Stack Overflow the
C# Throw Compiler Error
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags
#error Directive C#
Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only
#error In C# Example
takes a minute: Sign up Custom Attribute that can generate compile time error [duplicate] up vote 1 down vote favorite This question already has an answer here: Attribute to generate compilation error on method call? 1 answer I have an interface: public interface IBaseInterface { void Base(); } And I use use it like: public class BaseClass : IBaseInterface { public compilation error in c# void Base() { } } However, I want to implement an attribute such that: public class BaseClass : IBaseInterface // Generates Compile-Time Error { public void Base() { } } I only want to be able to use the interface like this: public interface IAdditionalBaseInterface : IBaseInterface { } public class AdditionalBaseClass : IAdditionalBaseInterface // No Compile-Time Error { public void Base() { } } Is it possible to have a custom Attribute which does this? I am aware of the ObsoleteAttribute but was wondering how this even works, or more so, how I can implement my own which has the same compile-time error functionality. c# attributes compiler-errors share|improve this question edited Nov 7 '13 at 18:22 asked Nov 7 '13 at 18:04 gleng 4,22331230 marked as duplicate by Trevor Elliott, Kate Gregory, Yan Sklyarenko, Werner Henze, Liam Nov 8 '13 at 15:34 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. Note that the ObsoleteAttribute cheats and uses compiler magic to ac
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Can one force a custom compiler error in c#? up vote 2 http://stackoverflow.com/questions/19843361/custom-attribute-that-can-generate-compile-time-error down vote favorite 1 I'm creating a library in C# that can be used within a transaction. However, some of its methods should not be used in a transaction as they cannot be rolled back. These non-transactional methods will throw an exception at run time when used within a transaction. The question is, can I somehow also force a compile time error when a non-transactional method is used within http://stackoverflow.com/questions/7409256/can-one-force-a-custom-compiler-error-in-c a transaction? Thank you in advance. Update I should clarify that the compile time errors are for the benefit of 3rd parties who will use my library. As such any solution that requires another framework or extension would not suffice. The ideal solution would stop the project from compiling in Visual Studio. c# share|improve this question edited Sep 13 '11 at 23:29 asked Sep 13 '11 at 22:20 flc 133 add a comment| 2 Answers 2 active oldest votes up vote 4 down vote accepted That's impossible. It is not possible to reliably determine at compile-time whether you're in a transaction. Consider the following code: if (...) thingy.BeginTransaction(); thingy.NonTransactionalMethod(); share|improve this answer edited Sep 13 '11 at 22:26 answered Sep 13 '11 at 22:24 SLaks 540k9313471516 add a comment| up vote 3 down vote I don't think it's possible with C#, but it might be using Microsoft's Code Contracts. Code Contracts: http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx You might also take a look at Spec#: http://research.microsoft.com/en-us/projects/specsharp/ EDIT Thanks to @Jonathan Dickinson for his suggestion - another option you might look into is FXCop: http://msdn.microsoft.com/en-us/library/bb429476(v=vs.80).aspx http://www.ademiller.com/blogs/tech/2009/06/making-sure-fxcop-warnings-and-errors-break-the-build/ share|improve this answer edited Sep 13 '11 at 22:44 answered Sep 13 '11 at 22:29 James Johnson 34.9k44585 1 I was going to say
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/1425806/how-to-throw-compilation-error-from-an-attribute or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x http://stackoverflow.com/questions/1420143/generating-a-custom-compile-time-warning-c-sharp Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to throw compilation error from an attribute? up vote 5 down vote favorite 1 there are attributes in .NET (e.g. ObsoleteAttribute) which are able to prevent compilation or at least throw compile time in c# warnings depending on their configuration. How to write an attribute that is able to interfere with compilation? Is there a way to do this from outside Microsoft? c# .net attributes share|improve this question asked Sep 15 '09 at 8:04 zzandy 9021231 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted Obsolete attribute is "compiler magic" - you can't do something like this on your own without modifying the compiler. However, as always, there are error in c# workarounds - e.g. take a look at this and this thread. share|improve this answer answered Sep 15 '09 at 8:08 Saulius Valatka 3,46341726 add a comment| up vote 3 down vote Using PostSharp: http://stackoverflow.com/questions/1420143/generating-a-custom-compile-time-warning-c/1420161#1420161 share|improve this answer answered Sep 15 '09 at 8:07 Frederik Gheysels 42.9k772133 PostSharp is a great hint, thanks. –zzandy Sep 15 '09 at 8:51 add a comment| up vote 0 down vote Any attributes that exhibit this inside the compiler are special cases. The [Obsolete] behaviour is described in the C# spec (17.4.3), for example. You could write a custom FxCop rule? share|improve this answer answered Sep 15 '09 at 8:15 Marc Gravell♦ 629k14617582227 That doesn't give you the opportunity to give you a compile time error/warning, does it ? –Frederik Gheysels Sep 15 '09 at 8:16 It does if you include FxCop in your build process. In VS2008 (team developer edition) you can do this either by declaring a build symbol, or clicking a checkbox on project properties... –Marc Gravell♦ Sep 15 '09 at 8:20 1 For info, it is the CODE_ANALYSIS symbol, or the checkbox on the "Code Analysis" tab. –Marc Gravell♦ Sep 15 '09 at 8:25 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a gue
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Generating a custom compile time warning C# up vote 18 down vote favorite 8 I'm using VS2008 and would like to create a compile time warning / error based on custom attributes on a property (if it is possible). There are two cases which interest me currently: [MyAttribute (typeof(MyClass)] Where MyClass has to implement an interface. Currently I assert this in the constructor of the attribute, however this doesn't make it easy to track down, due to the nature of the stack trace: public MyAttribute (Type MyClassType) { System.Diagnostics.Debug.Assert(typeof(MyInterface).IsAssignableFrom(MyClassType), "Editor must implement interface: " + typeof(MyInterface).Name); } The second case which interests me is where I have a type defined in an attribute, if that type implements an interface, then a warning should be displayed if another attribute isn't present. I.E. if (MyClass.Implements(SomeInterface) && !Exists(SomeAttibute)) { Generate Warning } [MyAttribute(typeof(MyClass)] // Comment next line to generate warning [Foo ("Bar")] Thanks! c# compiler-construction custom-attributes postsharp share|improve this question edited Jan 11 '12 at 12:55 Frederik Gheysels 42.9k772133 asked Sep 14 '09 at 7:32 Courtney D 1,91211627 Interesting question, I'm curious if such thing is possible. +1 –Clement Herreman Sep 14 '09 at 7:38 Interested as well. –matt_dev Mar 4 '10 at 0:01 stackoverflow.com/questions/154109/custom-compiler-warnings –Mauricio Scheffer Aug 16 '13 at 4:07 add a comment| 1 Answer 1 active oldest votes up vote 7 down vote accepted You can do that with PostSharp. I've once done it, and explained how to do it here share|improve this answer answered Sep 14 '09 at 7:39 Frederik Gheysels 42.9k772133 Thanks a lot for that! I had looked at post sharp before (for another problem) and decided it against it, however now I think I will be re-evaluating ;) –Courtney D Sep 15 '09 at 1:11 I have just discovered a bit of an issue - it seems that GetCustomA