I'm trying to check the validity of a particular 'stand-alone' code file from within my C#.Net app. Is there any way that I can check the file and get a visual studio's style errors list out?
I'm only interested in running a basic check to ensure that the basics would validate. I.e. all variables are declared and method names are valid.
Is this at all possible?
If it's not the same as the referred to question (see my comment), you mention "VS style errors", consider using the CSharpCodeProvider. An example of its usage is in its documentation at MSDN.
Call the C# command line compiler (csc.exe)?
You can use static code analysis. There are a few libraries that enable this. The one that cones to mind immediately is StyleCop. It is written in .NET so you should be able to reference and use it through code if that is what you are looking for.
Related
You load a foreign code example with libraries attached to it in Visual Studio. Now there is a method that you want to reuse in your code. Is there a function in VS that lets you strip the code from all unnecessary code to only have code left that is necessary for your current method to run?
It is not about the library. Loading a .sln or .csproj and having classes over classes when you just want one method out of it is a waste of performance, ram and space. It is about code you can easily omit or references(what I call libraries) you can easily omit. A part-question of this is: Which "using" statement do you need that is only necessary for your current method and the methods that pass paramaters to it? In short, showing relevant code only. Code that is tied to each other.
Let's use an example: You go to github and download source code in c#. Let's call the solution S. You open S in Visual Studio. You don't disassemble, you just load the source code of S, that is there in plain text. Then you find a method M - in plain text - that you want to use. M contains some objects whose classes were defined somewhere in the project. The goal is to recreate the surrounding only for this method to copy & paste it into my own solution without having red underlined words in almost every line within the method
after reading the question and the comments, I think I have a vague idea what you are referring to.
In case we ignore the context of the method you are referring, you can extract any code piece from a "library" by using a .NET decompiler and assembly browser.
There are many of them for free, such as:
dotPeek,
ILSpy
...
This will allow you to see the method's code. From there on, you can proceed as you like. In case your copy the method to your code base, you might still have to change it a bit in order to adapt it to work with your objects and context. If you don't, this will give you insight on how the method works and might help you to understand the logic, so you can write your own.
Disclaimer: With this post, I am pointing out that it is possible to extract code from an assembly. I am not discussing the ethics or legal perspective behind such actions.
Hope this helps,
Happy Coding!
If it`s just one method, look at the source code and copy it to your libarary. Make sure you make a comment where you obtained the code and who has the copyright! Don't forget to include the licence, which you should have done with a libary reference anyway.
That said it is currently not (official) possible to automaticly remove unused public declared code from a library (assembly). This process is called Treeshaking by the way. Exception: .NET Native.
But .NET Native is only available for Windows Store Apps. You can read more about it here.
That said, we have the JIT (Just in Time)-Compiler which is realy smart. I wouldn't worry about a few KB library code. Spend your time optimizing your SQL Queries and other bottlenecks. The classes are only loaded, when you actualy use them.
Using some unstable solutions or maintaining a fork of a library, where you use more then one method (with no documentation and no expertise, since it is your own fork) isn't worth the headache, you will have!
If you realy want to go the route of removing everything you do not want, you can open the solution, declare everything as internal (search and replace is your friend) and restore the parts to public, which are giving you are Buildtime error / Runtime error (Reflection). Then remove everything which is internal. There are several DesignTime tools like Resharper, which can remove Dead Code.
But as I said, it's not worth it!
For .NET Core users, in 6-8 weeks, we have the .NET IL Linker as spender has commented, it looks promising. What does this mean? The .NET framework evolves from time to time. Let it envolve and look at your productivity in the meantime.
In a Visual Studio 2010 C# Project, how do I find out if any method is written without having a try-catch block?
Sometimes as a code reviewer, its hard to search function/methods which are not properly written according to code standards esp: without a try-catch block.
I know that the Find option in Visual Studio supports regular expressions. But what's the regular expression that could perform this job smartly?
Edit (putting the direct answer first): It would actually be easy to use Assembly.ReflectionOnlyLoadFrom, then enumerate the types, and the methods of those types, then for each method body examine the ExceptionHandlingClauses.
Commentary follows:
Red Gate used to offer the Exception Hunter tool for tracking down possible exception issues. As mentioned on that page, it's been shown that the specific task you have requested (broad searches for any unhandled exception) does not lead to higher quality software even with assistance of automated analysis tools.
If I needed to find a list of all methods without a protected region, I could simply use one of my experimental assembly loaders (written with relative ease according to ECMA-335) and examine the metadata as described in ECMA-335, Partition II, §25.4.5 and §25.4.6 (much easier to use the first part of this answer). If I needed to find a list of these methods for the purpose of adding a protected region to each of them to conform to a coding standard, I would report back that they need to find another developer to do that because I refuse to knowingly and intentionally degrade the quality of software I work on.
If you have access to Visual Studio 2012 (the beta is a free download!), you can use the Search For Code Clones option.
Normally this will search your whole solution but you can scope it to a specific piece of code. You can easily use this to find all code where, for example, a disposable object is used. Then you can chek if a using statement is used everywhere.
This same mechanism can be used to check if a certain piece of code is used in a try/catch block.
Here is some MSDN documentation.
I think you could use Roslyn to process the code and find which methods have or don't have try/catch blocks by examining the expression tree.
I would not advise you to use Regex to do that. You'd have to write a quite complex script that would be a state machine based on when a method block starts and when it finishes, and then inspecting whether are try-catches in there.
I need to run small snippets of C# code for educational purposes and for each execution, I should open the project (solution), delete existing code, type new code, build and compile, and then run the project. For example, for executing string.IsNullOrEmpty("something") I should follow all this procedure.
I just thought of something like:
In which I can enter code snippets, click execute, and then see the result. Is there anyway to do that?
You didn't explicitly state if you're looking for guidance on implementing your own solution or what, but if you're open to using a third party utility then LINQPad is pretty much exactly what you're describing.
Take a look at Snippet Compiler.
Not exactly what you're asking for, but from what I can tell it seems pretty close.
There is also ideone which can run C# code, as well as a large number of other languages.
There is also mono's csharp/gsharp:
see http://www.mono-project.com/CsharpRepl
Mono are doing the compiler as service stuff - this is what you are looking for: http://tirania.org/blog/archive/2010/Apr-27.html
first of all I want to specified that formaly i'm a java programmer, but now i have to move for a while to C# programming in visual studio 2010 express for building a piece of program which read and write to smardcard (sle4428). And I have a couple of question.
1) I've seen some documentation about the winscard.dll api in this url, but in the signature of the function there aren't the data type of parameter but only in or out. (Doh) Exists a way to unterstand which type I have to use?
2)Exists some code example of how initialize a communication, without searching in "google search code"
3) The custumer provided to me basic VB code, can I build something which I can use in a C# context?
Thnaks guys for any help!!!!
Hope this helps:
http://pastie.org/1527598
The URL you've provided actually does show the types. LONG WINAPI SCardGetStatusChange( __in SCARDCONTEXT hContext, ... means that the first argument is formally named hContext, and has type SCARDCONTEXT.
The same page links to another page with an example.
Whether the VB code is useful at all depends on whether it's VB.Net. That would show the correct P/invoke forms.
I am researching ways, tools and techniques to parse code files in order to support syntax highlighting and intellisence in an editor written in c#.
Does anyone have any ideas/patterns & practices/tools/techiques for that.
EDIT: A nice source of info for anyone interested:
Parsing beyond Context-free grammars
ISBN 978-3-642-14845-3
My favourite parser for C# is Irony: http://irony.codeplex.com/ - i have used it a couple of times with great success
Here is a wikipedia page listing many more: http://en.wikipedia.org/wiki/Compiler-compiler
There are two basic aproaches:
1) Parse the entire solution and everything it references so you understand all the types involved in the code
2) Parse locally and do your best to guess what types etc are.
The trouble with (2) is that you have to guess, and in some circumstances you just can't tell from a code snippet exactly what everything is. But if you're happy with the sort oif syntax highlighting shown on (e.g.) Stack Overflow, then this approach is easy and quite effective.
To do (1) then you need to do one of (in decreasing order of difficulty):
Parse all the source code. Not possible if you reference 3rd party assemblies.
Use reflection on the compiled code to garner type information you can use when parsing the source.
Use the host IDE's (if avaiable - so not applicable in your case!) code element interfaces to provide the information you need
You could take a look at how http://www.icsharpcode.net/ did it. They wrote a book doing just that, Dissecting a C# Application: Inside SharpDevelop, it even has a chapter called
Implement a parser to provide syntax
highlighting and auto-completion as
users type